Closed
Description
Try to send request with content type application/x-www-form-urlencoded
var data = Querystring.stringify({
"grant_type": "password",
"username": login,
"password": password
});
axios.post(Urls.login, data);
but there is no such header in request.
I tried to use code:
var data = Querystring.stringify({
"grant_type": "password",
"username": login,
"password": password
});
return axios({
method: 'post',
url: Urls.login,
data: data,
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
});
same problem
jquery code works fine:
$.ajax({
url: Urls.login,
data: data,
type: "POST",
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
});
How can I use axios to send request with this header?
Activity
mzabriskie commentedon Jun 28, 2016
That should work as you've shown. Sounds like a bug. I will look into it.
alborozd commentedon Jun 29, 2016
The cause is interceptors. I can't send request with that Content-Type only when I use interceptors.
Here is my code:
Inside interceptor I can see 'Content-Type' header, but it is not sent to the server.
Do I correctly use interceptors?
rahuljiresal commentedon Jun 30, 2016
Any updates on this? I have the same issue. Axios doesn't send the header I set.
rahuljiresal commentedon Jun 30, 2016
This seems to be the culprit line → https://github.com/mzabriskie/axios/blob/master/lib/adapters/xhr.js#L117
Any idea why the
Content-Type
header is removed before the request is sent?A workaround option is to change your
data
todata || {}
when you're making an axios request. This will make sure data is not undefined.mzabriskie commentedon Jun 30, 2016
It looks like the logic for removing
Content-Type
whenrequestData
isundefined
came in this commit 9096d34. There is no indication why it was added however.I would vote if the
requestData
isundefined
and method isPUT
,POST
orPATCH
and noContent-Type
has been set, then defaultContent-Type
toapplication/x-www-form-urlencoded
. Otherwise just honor whatever has been specified.nickuraltsev commentedon Jun 30, 2016
@mzabriskie But in the code snippet provided by @alborozd, the
data
is set toQuerystring.stringify({...})
, sorequestData
should not beundefined
, right?alborozd commentedon Jul 1, 2016
@mzabriskie I think you're right. When I use request interceptor fiddler shows that data is empty. Without interceptor I can see data and header and it works fine.
So, probably the problem occurs when you work with interceptors.
damianprzygodzki commentedon Jul 16, 2016
No interceptor is needed to crash this thing. I've set content type header defaults
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
, and i can't send any payload in POST body.I've used workaround with URLSearchParams:
But this is not supported by Safari and IE.
Edit: Okay :) Next update on workaround. Full supported workaround is send data as query string.
data: "flashcard=" + JSON.stringify(flashcard) + "&stackUri=" + stackUri
. It hurts, but it works 👍nickuraltsev commentedon Jul 25, 2016
You can use a library like qs instead:
alborozd commentedon Jul 25, 2016
this is not a solution. What is difference if I will use
"Querystring"
or"qs"
? The problem is the header['Content-Type'] = 'application/x-www-form-urlencoded'
is empty because of interceptors.damianprzygodzki commentedon Dec 8, 2016
Any updates here? Because i've lost 1h today on researching why my POSTs are not affecting API (til i reminded about that issue)... Or there are no plans to fix that and it's better to go somewhere else?
Dunkat commentedon Dec 8, 2016
I have the same problem... still waiting for fix...
cadavre commentedon Dec 8, 2016
Please reopen @nickuraltsev as this is not fixed by your solution.
+1 for issue.
229 remaining items