question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding headers to axios.post method

See original GitHub issue

Hi,

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:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

242reactions
ofieshcommented, May 17, 2017

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.

post("http://localhost:8080/foo", foo)

vs

post("http://localhost:8080/foo", foo, {
    headers: { Authorization: "Bearer " + token }
})
88reactions
jffernandezcommented, May 3, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found