0

I am trying to request an API using fetch in javascript. I did it in the simplest way, just declaring the method I am using and the header with the api key and auth.:

fetch(url, {
    method: "GET",
    headers: {
        "Authorization": "Bearer 0123456789",
        "x-api-key": "9876543210"
    }
})

The request sent by the browser changes the original fetch request:

  • The method sent is "OPTIONS" instead of "GET".
  • The header parameters (api key and auth) are included on "Access-Control-Request-Header" instead to be included directly on the header.

see how it is sent by the browser

How can I avoid the browser to change the method and keep the api key and auth. directly on the request header?

Thanks a lot

Dunick
  • 111
  • 1
  • 10
  • 1
    This is a preflight thing afaik; the browser is making sure that the server supports the request you want to send. The actual GET request should follow if the OPTIONS request goes through. –  May 11 '20 at 12:09
  • 1
    Does this answer your question? [Why am I getting an OPTIONS request instead of a GET request?](https://stackoverflow.com/questions/1256593/why-am-i-getting-an-options-request-instead-of-a-get-request) –  May 11 '20 at 12:11

1 Answers1

0

The answer is as written: this is a preflight request. Issue has been explained. Further infos on Why am I getting an OPTIONS request instead of a GET request?

Dunick
  • 111
  • 1
  • 10