-
-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Setting header: 'Content-Type': 'application/json' is not working #86
Comments
There was an issue with Content-Type having to be cased correctly (see #89). I just fixed it a few minutes ago. Maybe this was the same issue you were experiencing? |
Well, I wasn't able to send the header 'Content-type' at all on my request. @mzabriskie let me give it a try and I'll let you know. Thanks. |
This doesn't appear to be working for me, either. It looks like perhaps it's this line here? https://github.com/mzabriskie/axios/blob/master/lib/adapters/xhr.js#L77 |
Is your request body empty by any chance? |
Yeah, the request body was empty. On closer inspection, it appears that Axios is doing the right thing here. Content-Type only applies to the request body, so if it's missing then it shouldn't pass along that header. |
Yes it is correct to remove the headers, you need to pass on an empty body though (IE does not do that, it passes "undefined" which triggers errors in some/most backends). We are using Restler in a project to serve JSON and Restler was throwing 403 errors because the "undefined" string gets passed as Content-Type: text/plain which doesn't work. |
Yep, I can confirm. If there is no |
I'd vote for this issue to be closed, then. @alejandronanez what do you think? |
This stills an issue. You need to specify the data:null or data:{} to get it to work :-) |
@teleyinex |
@mcmire then I agree completely. But, it might be good to add this to the docs, so others can easily find it. |
Yeah, agreed about that, it's definitely something worth documenting. |
One example in the README will do the trick :D |
|
Just for other people Googling, this is how I had to format my axios.post request
|
Is there a particular reason to remove content-type in this line if data is not provided?
|
Thanks for clarifying @dinodsaurus. |
Same issue successfully fixed with |
@SirSerje While it is probably a good idea to set a default This bears repeating: if you're here and you want |
@mcmire I've just needed to fix the hole quickly. Now I can go by more sophisticated way, thank you for clarification. |
If a user of this package wants to set a |
This comes up for AWS API Gateway endpoints - you want to do a PUT request to an endpoint that expects content type of application/json and the request doesn't work unless you send an empty body, which is contrary to how a PUT request should be sent... |
This is still an issue. I have to do this if I want the content type to be application/json
|
Something weird, or just something related to rails, but when i append Was struggling for some time on this issue. |
Reopen the issue |
@ziaulrehman40 @alejandronanez It seems you guys are using Rails and expect Rails to respond with JSON. The problem is that you're setting the wrong HTTP header to accept JSON as a response. The header you should set is axios.get('my/url', {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
}) |
I found a workaround by simply setting empty data in the request, so Axios assumed the Hope this helps.
|
this example fixed for me the issue
|
Hi. I'm passing a custom header like this
But it doesn't seem to work. Any ideas?
The text was updated successfully, but these errors were encountered: