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 application/x-www-form-urlencoded #362
Comments
That should work as you've shown. Sounds like a bug. I will look into it. |
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. |
Any updates on this? I have the same issue. Axios doesn't send the header I set. |
This seems to be the culprit line → https://github.com/mzabriskie/axios/blob/master/lib/adapters/xhr.js#L117 Any idea why the A workaround option is to change your |
It looks like the logic for removing I would vote if the |
@mzabriskie But in the code snippet provided by @alborozd, the |
@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. |
No interceptor is needed to crash this thing. I've set content type header defaults 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. |
You can use a library like qs instead: var qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 }); |
this is not a solution. What is difference if I will use |
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? |
I have the same problem... still waiting for fix... |
Please reopen @nickuraltsev as this is not fixed by your solution. +1 for issue. |
I use a interceptor with qs library to solve this problem. Works fine for me.
|
+1 for issue. |
Great, but it is not a way to solve issue in libraries IMHO by installing another. |
You can just add a This is what I did, and it worked for me:
This changed the |
Has this been fixed yet? I can't seem to set my Content-Type with any of the solutions posted here. |
I have same problem, any help? |
I solved the problem using encodeURIComponent: static getToken(username, password) { |
bump. One would definitely assume that if one sets defaults, they would be always be respected. Axios is definitely ignoring the defaults in certain contexts, causing issues for poor folks. |
Here's a more specific merge where this happened: https://github.com/mzabriskie/axios/pull/195/files |
+1 for this issue. |
@polyakoff how did you solve for this?or are you still stuck. What I have observed is that this issue happens intermittently |
@usopan Still stuck. |
I moved to isomorphic-fetch for this particular request as a workaround. |
Got the same issue. And if I set the key to anything else, it works, except for "Content-Type"! Please help! |
I've actually had to create yet another messy hybrid app by using fetch in browsers and axios in node and within react-native. Seems funny that it's like this and not the other way around. Really hoping to see this tackled soon so I can remove my monkey-patch. I think it's important to realise that this is definitely not only an issue with querystring.. My body content is just raw text with no parameters, yet I can't send it using axios with a |
I tried using post, it works fine, default of post is json |
I'm experiencing this bug right now... so there Is no solution for this after years? wow... |
I created a PR that would fix the problem over two months ago... I don’t understand why it not get merged?! |
Keep in mind that no one has pushed anything since september last year, maybe they're searching for maintainers?. Also, I think you missed a test when I compare your PR with: https://github.com/axios/axios/pull/1544/files @mzabriskie could you maybe take the responsibility to merge one of these PR's? As currently some software requires get requests to have a content-type set (RoR params for instance: https://guides.rubyonrails.org/api_app.html#using-actiondispatch-request). The solution specified in #362 (comment) seems like the proper way to go and it would most of all solve all the desperate hacks like using fetch for this specific use case. |
I moved to |
So Axios is officially unable to send requests with 'Content-Type': 'application/x-www-form-urlencoded' or does @ChBernat solution actually work? |
Is there any workaround on this issue atm? |
Lookup a year up here and you will see plenty of workarounds... Though I would recommend, like others and myself included, move on from axios. It's abandoned... |
Wow... they should just abandon the project at this point, I know I am. Almost 3 years since issue originally reported and now we're still trying to solve this thing? Unbelievable. I love open source so I'm not harboring any ill feelings for this project not have maintainers but... it's used by TONS of people so the least you could do is abandon it so we all know this project is dead/dying. Thanks. |
@justintime4tea Do you have a new recommendation?
|
@kslr got or request seems like what everyone else has started migrating too. I'm trying to stick with Axios but I think it'll end up just coming down to me writing some abstraction layer over it so I can swap out the underlying HTTP request library and then if I use a different library which has a "built-in" HTTP client it uses, just let it do its thing. |
I tried something like this and worked for me.
I have removed the header, and it seems like it has received well. |
axios({
url,
method: 'POST',
headers:{
'Content-Type': 'application/json'
},
data: null, // important
}) |
I had two problems using RoR as backend. My context: I had a project using deprecated But, we had some problems and below I will share what I do to solve these problems. Maybe help someone! So sorry if I haven't helped you as you expected First problem, the problem is the same as this issue, I solved using: axios.interceptors.request.use(config => {
config.paramsSerializer = params => qs.stringify(params);
return config;
}); Now, params like: q: {
search: "Something",
sort: "asc",
} will be transformed to: this parsed will be:
Second problem, RoR is returning HTML response instead of JSON response: This problem occurs because our backend must distinguish AJAX requests from other types of requests. By default, axios.interceptors.request.use(config => {
config.paramsSerializer = params => qs.stringify(params);
return config;
});
axios.defaults.headers.common['Accept'] = 'application/json';
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; === If you use Rails as backend and no one of the solutions worked for you, let me know, maybe I help you =). |
Am working on react-native and getting bad request 400 error when use this : |
so,how to resolve this problem? |
any solution :( |
Solved By qs
|
Sad to see that so many people are confused in this issue. I have try my best to read those comments. But it seems to have involved many other problems. Everyone, forgive me to close it. If someone can open a new issue to describe the problem clearly, that will be really big thanks to you. |
Hey everyone, I thought I'd chime in as I wasted almost a full working day of my life trying to solve this. This may help some partially, or completely so here's to hoping this is useful. My reply stems from seeing various reasons why a POST was failing with Axios ranging from:
I was facing the same issue as everyone in this thread with the Bad Request 400 when passing in custom headers to Axios in the config like this, and tried many of the responses here, like using qs.stringify(), setting data in the config to null or {} to no avail.
Now my issue was the api POST body was not what the endpoint wanted, but I couldn't see it until I was able to log the error. Further, I'm also using NestJS and the HttpService, which is a wrapper around Axios, and that further complicated the issue because the real error wasn't bubbling up in the catchError callback in my pipe. So here is the solution for both pure Axios and NestJS. Axios:
NestJS:
TLDR;
Sorry for the long post and good luck everyone! Cheers |
this work for me. with query stringfy thanks a lot. |
Your solution is working. Thanks a lot! |
Try to send request with content type application/x-www-form-urlencoded
but there is no such header in request.
I tried to use code:
same problem
jquery code works fine:
How can I use axios to send request with this header?
The text was updated successfully, but these errors were encountered: