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.

Custom header not being sent during OPTIONS request.

See original GitHub issue

Describe the issue

Axios not sending custom header during OPTIONS request.

I use axios 0.18.0 in my Vue.js application that is running in e.g. cms.abc.com. and the server is running on APIGee with url server.abc.com. When I check the logs from APIGee I’m not receiving the value of x-api-key on OPTIONS request.

Example Code

 const headers = {
      'X-API-Key': '123123x123123'
    }

    return axios({
      method: 'get',
      url: 'https://server.abc.com/content/v1/deals',
      headers: headers
    }).then(response => {
      console.log(response.data)
      return response.data;
    })

Expected behavior, if applicable

I’m expecting that axios is also sending the x-api-key during OPTIONS request. It only sends during GET request. Not sure if browsers really not sending custom headers during pre-flight request.

Environment

  • Axios Version 0.18.0
  • Adapter HTTP
  • Browser Chrome
  • Browser Version: Version 87.0.4280.88
  • Node.js Version: 12.7.0
  • OS: OSX 11.0.1
  • Additional Library Versions Nuxt 2.13.3

Additional context/Screenshots

Screenshot 2020-12-09 at 4 30 35 PM

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
knksmith57commented, Oct 22, 2021

Unfortunately this isn’t something axios can fix —at least not in this case.

In browser environments, CORS-preflight requests are initiated and entirely managed by the user agent; the preflight operations are completely opaque to our userland code and axios has no ability to add additional headers to the outgoing OPTIONS request (because it isn’t making it).

If you’re fortunate enough to control the backend (APIGee server at server.abc.com in this case), the solution is to update the server code to correctly handle CORS-preflight requests; I recommend this great explainer from MDN as a starting point.

1reaction
SmirnovM91commented, Feb 3, 2021

I have the same problem, any solutions?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you send a custom header in a CORS preflight ...
This is by design; the purpose of the preflight request is to determine what information the useragent (browser) is permitted to send beyond...
Read more >
Access-Control-Allow-Headers - HTTP - MDN Web Docs
It indicates that a custom header named X-Custom-Header is supported by CORS requests to the server (in addition to the CORS-safelisted ...
Read more >
How do I add custom header to pre-flight OPTIONS request?
I am making a GET request to API that requires authorization header with key. Since it is cross-domain request there is pre-flight request...
Read more >
Chapter 4. Handling preflight requests - CORS in Action
It contains information like which HTTP method is used, as well as if any custom HTTP headers are present. The preflight gives the...
Read more >
Techniques for bypassing CORS Preflight Requests to ...
This Preflight request is dispatched transparently by the browser -- JavaScript code does not explicitly enact the Preflight OPTIONS request ...
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