Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content type changed from multipart/form-data to application/x-www-form-urlencoded #5067

Closed
Flamenco opened this issue Oct 8, 2022 · 10 comments · Fixed by #5224
Closed

Content type changed from multipart/form-data to application/x-www-form-urlencoded #5067

Flamenco opened this issue Oct 8, 2022 · 10 comments · Fixed by #5224

Comments

@Flamenco
Copy link

Flamenco commented Oct 8, 2022

My content-type header of multipart/form-data is getting replaced with another content-type. This was not an issue until I switched to v1.0.x

const api = axios.create({
 headers: {
  'Content-Type': 'application/json'
 }
})

const formData = new FormData()
formData.append('file', file)
api.post('endpoint', formData, {
  headers: {
   'Content-Type': 'multipart/form-data'
 }
})

However, this works:

axios({
 url: 'endpoint',
 method: 'post',
 data: formData,
 headers: {
   'Content-Type': 'multipart/form-data'
 }
})

This was sent with the old version, and the workaround mentioned:
Screen Shot 2022-10-08 at 4 46 55 PM

And this started happening when I upgraded.

Screen Shot 2022-10-08 at 4 46 40 PM

@Flamenco Flamenco changed the title Content type changed from multipart/form-data to application/x-www-form-ulrencoded Content type changed from multipart/form-data to application/x-www-form-urlencoded Oct 8, 2022
@GamalC
Copy link

GamalC commented Oct 10, 2022

I believe that I had to adjust to some changes made in headers processing as well. Adding here in case it helps someone.

I am sending an Axios POST request with the following data:
var data = {'key1': value1, 'key2': value2 };

And had Headers:
var default_headers = { ... 'Content-type': 'application/x-www-form-urlencoded', ...};

In version 0.22.0, the decoded content was:
{"key1":"value1","key2":"value2"}

In version 1.0.0+ it is now:
key1=value1&key2=value2

The update now seems to be the correct processing if Content-Type is set to 'application/json' (which now seems to work in 1.0.0+) but it initially caused a crash after updating when I tried to load the JSON.

@physicsSorcererKing
Copy link

@Flamenco Don't add 'Content-Type': 'multipart/form-data' by yourself before this issue is solved.
axios will automatically do this if config.data's type is FormData
see https://github.com/axios/axios/tree/main#formdata

@DigitalBrainJS
Copy link
Collaborator

Can you reproduce the issue here?

@Flamenco
Copy link
Author

Flamenco commented Oct 14, 2022

@physicsSorcererKing I am adding it because my base axios, that had all sorts of headers and logic, declared content type of "application/json", as shown. I need to override it. Perhaps overriding it as 'undefined' might work.

@physicsSorcererKing
Copy link

@DigitalBrainJS I found I spelled lowercase content-type caused it happened.
Not sure is this @Flamenco 's case.

@DigitalBrainJS
Copy link
Collaborator

OK. I found the problem, so I will fix it soon.

@jasonsaayman
Copy link
Member

jasonsaayman commented Nov 10, 2022

Hi 👋

Please try the latest pre-release by running the following:

npm i axios@1.2.0-alpha.1

Please provide feedback in either the pinned issue or discussion thread 🧵

@FelipeEduardoR2
Copy link

@jasonsaayman
Eu testei e no meu caso resolveu!
(I tested and it's ok!)

@darkvertex
Copy link

I also had an issue where my headers were getting ignored during a multipart formdata call and Axios was submitting it as a urlencoded form data, which my backend did not likey.

axios@1.2.0-alpha.1 fixed this behaviour for me and my multipart calls work again. 🎉

@Soneji
Copy link

Soneji commented Nov 26, 2022

Same issue, axios 1.2.0 is out now and fixes this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants