Adding headers to axios.post method
See original GitHub issueHi,
I tried to make a post request to cross-domain IP and my code looks like;
var authOptions = {
method: 'POST',
url: 'http://10.254.147.184:7777/auth/oauth/token',
data: qs.stringify(data),
headers: {
'Authorization': 'Basic Y2xpZW50OnNlY3JldA==',
'Content-Type': 'application/x-www-form-urlencoded'
},
json: true
};
return dispatch => {
return axios(authOptions)
.then(function(response){
console.log(response.data);
console.log(response.status);
})
.catch(function(error){
console.log(error);
});
But whenever I add ‘headers’ to it, my request automatically turns into ‘OPTIONS’ from ‘POST’ method and I don’t know why. Is there a bug about it or anything I couldn’t find?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:17 (1 by maintainers)
Top Results From Across the Web
Passing headers with axios POST request - Stack Overflow
When you are using the Axios library and to pass custom headers, you need to construct headers as an object with the key...
Read more >How to Send Headers With an Axios POST ... - Stack Abuse
Axios is an HTTP client library that is used to send asynchronous HTTP requests such as POST , GET , and DELETE to...
Read more >Using Axios to set request headers - LogRocket Blog
Axios methods such as post() and get() enable us to attach headers to requests by supplying a headers' object as the second parameter...
Read more >How To Set Request Headers Using Axios? - RapidAPI
Your application may have multiple API requests, and you may want to set request headers for all of them. Instead of adding the...
Read more >Axios - HTTP POST Request Examples | Jason Watmore's Blog
This sends the same POST request again using axios with a couple of headers set, the HTTP Authorization header and a custom header...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Edit: I had to add Authorization to allowed headers in my CORS filter
@jffernandez
I’m having the same issue. When I leave out the Auth header I’m getting an Options request which returns POST, OPTIONS and then the POST which returns a 403 because it’s missing the Authorization header (expected).
When I add the header I just get the option request and it never makes the POST.
vs
This is not a bug, it’s working as expected. You are generating a preflighted request (not a simple one) as stated in the docs https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS because you are adding a header and a content-type that forces this. So, in this mode, an OPTIONS request is issued before your main request, you must ensure the server can manage the OPTIONS request for it to work.