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

Can not change Contant-Type when data is FormData #1603

Open
TabrisK opened this issue Jun 13, 2018 · 16 comments · Fixed by #1773 or #3785
Open

Can not change Contant-Type when data is FormData #1603

TabrisK opened this issue Jun 13, 2018 · 16 comments · Fixed by #1773 or #3785

Comments

@TabrisK
Copy link

TabrisK commented Jun 13, 2018

axios({
                method: "PUT",
                url: url,
                transformRequest: [function (data, headers) {
                    // Do whatever you want to transform the data
                    return formData;
                }],
                headers: {
                    'Content-Type': file.type
                },
                "withCredentials": false
            });

Launched request with Content-Type:
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryPz13ZA77aLqgPlwB

@Oceanswave
Copy link

Looks like when axios detects form data in the request body, it auto-sets a multipart/form-data header, overriding the header value.

This gets tricky if you're mocking responses from the server with something like Nock, which will see a different boundary value in the response -or- needing to interact with a api that expects a multipart/related content type.

@ppozniak
Copy link

I am just facing the same issue.
Have you found any way to solve this @TabrisK

@TabrisK
Copy link
Author

TabrisK commented Feb 28, 2019

@ppozniak
Unfortunately, I can't change the behavior of axios. Finally I solved the problem by using XMLHttpRequest.

@ppozniak
Copy link

ppozniak commented Apr 3, 2019

delete requestHeaders['Content-Type']; // Let the browser set it

This is probably the cause.

@ppozniak
Copy link

ppozniak commented Apr 3, 2019

One really ugly hack, without rewriting the app would be to hack object's prototype. This will make Content-type stay as it is.

transformRequest: payload => {
       // ...
        Object.setPrototypeOf(formData, null);
        return formData;
      },

@jasonsaayman
Copy link
Member

Fixed with #1773

@chinesedfan
Copy link
Collaborator

@jasonsaayman I am afraid #1773 didn't solve it. Reopen for confirming.

@Masaokb
Copy link

Masaokb commented Oct 25, 2020

Has this issue been solved?

In my case, using axios.request, content-type header disappeared even when I specified, but using axios.post didn't seem to have any problem.

@manamika
Copy link

manamika commented Nov 3, 2020

In my case, i need to override the content-type to have a version - Content-Type: multipart/form-data;version=2.0.0;boundary=B_someBoundary. Is this something that can be achieved?

@Kalinin
Copy link

Kalinin commented Mar 4, 2021

I have the same bug as described in issue. Still happens.

@YsBunkerchain
Copy link

The Get method also not able to change the content-type when I put the data as FormData.

@shivanshr
Copy link

I am not able to do so with POST(mulitpart/form-data) request, I can not set header['Content-Type'] 'mulitpart/form-data', it always changes to 'text/plain;charset=UTF-8'.
"axios": "^0.21.1",
"react-native": "0.63.4"

@norbertsongin
Copy link
Contributor

It's because of this part https://github.com/axios/axios/blob/master/lib/adapters/xhr.js#L18-L20
Looks like it was first reported in 2017 #767 but was closed without being fixed

Removing this part worked for me

@JonathanSchndr
Copy link

:( same issue here in nuxt-auth with axios as dep.

@yoyo837
Copy link

yoyo837 commented Apr 29, 2022

The fix has been reverted, this may require a reopen.

@jasonsaayman
Copy link
Member

Yeah this was reverted sorry about that re-opening, but we will need to approach this better

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