Setting header: 'Content-Type': 'application/json' is not working
Hi. I'm passing a custom header like this
axios.get('my/url', {
headers: {
'Content-Type': 'application/json'
}
})
But it doesn't seem to work. Any ideas?
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? For me manually passing data: null to axios, in order to manually set an empty body, prevented IE from sending undefined as body.
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 data passed it removes the Content-Type...
I just added data: {} to my GET request, and it works fine.
It's really weird to pass data on get requests but I don't mind :+1:
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 Content-Type describes what format the request data is in. If there is no request data, there is no need to specify the Content-Type. So you can correct me if I'm wrong but I believe the present behavior makes sense, aside from the fact that it should still not add the header if data is null. What do you think?
@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
SignIn = () => {
console.log('login clicked')
let data = JSON.stringify({
password: this.state.password,
username: this.state.email
})
axios.post('url', data, {
headers: {
'Content-Type': 'application/json',
}
}
)
}
Just for other people Googling, this is how I had to format my axios.post request
var postData = {
email: "[email protected]",
password: "password"
};
let axiosConfig = {
headers: {
'Content-Type': 'application/json;charset=UTF-8',
"Access-Control-Allow-Origin": "*",
}
};
axios.post('http://<host>:<port>/<path>', postData, axiosConfig)
.then((res) => {
console.log("RESPONSE RECEIVED: ", res);
})
.catch((err) => {
console.log("AXIOS ERROR: ", err);
})
Is there a particular reason to remove content-type in this line if data is not provided? https://github.com/axios/axios/blob/master/lib/adapters/xhr.js#L135 Why my get request can't have content-type: application/json? This is super strange to write everywhere
await axios.get(url, {data: null})
@EJIqpEP check what @mcmire said.. so basically you need to handle this on your backend
Thanks for clarifying @dinodsaurus.
const config = {
headers: {
accept: 'application/json',
},
data: {},
};
Same issue successfully fixed with data: {}. I hope, it will work 'from the box' soon.
@SirSerje While it is probably a good idea to set a default Accept header, I just want to point out that the Accept header isn't the same as Content-Type. The Content-Type tells your server the format you, as the client, are sending the server. Accept tells your server the format in which you, as the client, want response data.
This bears repeating: if you're here and you want Content-Type to always be set because you're using it on the server side to figure out which format to send data back, then that's incorrect; you want to be using Accept instead. See this SO question.