Axios FormData upload broken in v2
Prerequisites
- [X] I confirm my issue is not in the opened issues
- [X] I confirm the Frequently Asked Questions didn't contain the answer to my issue
Environment check
- [X] I'm using the latest
mswversion - [X] I'm using Node.js version 18 or higher
Node.js version
v18.18.2
Reproduction repository
https://github.com/pschyma/msw-examples/tree/axios-formdata-reproducer
Reproduction steps
cd examples/with-vitest
npm install
npm test
Current behavior
I'm not sure where the issue is, but as msw v2 triggered this issue for me, I'll start here.
After updating to msw 2 FormData upload via Axios stops working due to missing boundary in Content-Type header.
Headers observed when posting data with fetch:
{
'content-type' => {
name: 'content-type',
value: 'multipart/form-data; boundary=----formdata-undici-088515513855'
}
}
Headers observed when posting data with Axios:
{
'content-type' => {
name: 'content-type',
value: 'multipart/form-data'
}
}
Expected behavior
Axios requests behave like when msw < 2 is used and provide the boundary in headers.
In previous msw version, I used parse-multipart-data to extract the boundary from request headers and for parsing the text content of that request.
I believe there is a CVE based on why you are no longer seeing this data: https://github.com/axios/axios/issues/6006
axios 1.5.0 was the last working version with msw, and the axios 1.5.1 update breaks our Testing.
Hi, @pschyma. Thanks for reporting this.
I looked at your reproduction example and it looks okay. More to that, if it works with fetch and breaks with Axios, then I assume this is an Axios' issue.
MSW does not control/polyfill/affect/modify the FormData behavior. It's implemented by undici in Node.js and you can see that the same Undici (global fetch) appends the boundary to the Content-Type header correctly.
Our integration test for FormData is passing so it's unlikely there's an issue with MSW.
I recommend you create an issue in the Axios repo and share the team's response with us here so others can learn from it too. Thanks.
Hi, thanks for the quick reaction.
I recommend you create an issue in the Axios repo and share the team's response with us here so others can learn from it too. Thanks.
Done. The Axios team adapted their code, so everything should be working again when the change gets released.
Incredible collaboration! Thank you, @pschyma!