Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The "Authorization" header is not sent in OPTIONS call by the browser

I have a very simple HTTP call:

fetch('http://127.0.0.1:5500/v1/homes/', {
  method: "GET", 
  mode: "cors", 
  credentials: "same-origin", 
  headers: {
     Authorization: "TEST"
  }})
  .then(function(response) {
    console.log(response);
  });

But when I look into the "Networks" tab of Google Chrome DevTools, the OPTIONS request does not have the "Authorization" header in it. This causes the server to replies with 401 Unauthorized.

The browser's curl equivalent of the request:

curl 'http://127.0.0.1:5500/v1/homes/' \
-X OPTIONS -H 'Access-Control-Request-Method: GET' \
-H 'Origin: http://localhost:8100'
-H 'Referer: http://localhost:8100/'
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
-H 'Access-Control-Request-Headers: authorization'
--compressed

Am I doing something wrong in the client side or is it quite common that browsers do not send the Authorization header in OPTIONS request, and therefore, I need to change the server in a way to response to OPTIONS call without requiring Authorization header?

like image 345
bman Avatar asked Aug 29 '18 07:08

bman


People also ask

Does browser automatically send Authorization header?

The HTTP Authorization request header can be used to provide credentials that authenticate a user agent with a server, allowing access to a protected resource. The Authorization header is usually, but not always, sent after the user agent first attempts to request a protected resource without credentials.

How do I fix the Authorization of the header is missing?

This error means that your WordPress Permalink rules are not up-to-date. To fix the issue, you need to update the Permalink rules in your site's . htaccess file.

How do I view the Authorization header in Chrome?

In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.


2 Answers

is it quite common that browsers do not send the Authorization header in OPTIONS request

More than common. It is required by the CORS spec which says "for a cross-origin request with preflight … make a preflight request … Exclude user credentials".

I need to change the server in a way to response to OPTIONS call without requiring Authorization header?

Yes

like image 153
Quentin Avatar answered Sep 28 '22 06:09

Quentin


An Options call is requested by the client, in your case Chrome browser implicitly before the actual GET call.

From MDN

The HTTP OPTIONS method is used to describe the communication options for the target resource. The client can specify a URL for the OPTIONS method, or an asterisk (*) to refer to the entire server.

On the server side, you'll have to intercept this Options request and respond back with a HTTP Status code of 200 and a Allow header indicating the operations that are permitted on this resource. eg: Allow: HEAD,GET,PUT,DELETE,OPTIONS

The browser on receiving these details will then proceed with the Get call.

like image 37
SubSul Avatar answered Sep 28 '22 05:09

SubSul