n4gys4nyi
n4gys4nyi

Reputation: 933

custom headers are not added to Request object

I'm trying to use fetch api.

First i create a new Headers() object:

var oHeaders = new Headers({
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    "X-DocuSign-Authentication": '{"Username":"xxx","Password":"xxx","IntegratorKey":"xxx"}'
})

After headers is instantiated if i try to log headers everything is correct.

oHeaders.forEach(function(v){console.log(v)})
//logs: 2 application/json {"Username":"xxx","Password":"xxx","IntegratorKey":"xxx"}

the i create the Request object:

var oReq = new Request('https://eu.docusign.net/restapi/v2/login_information', {
  method: 'GET',
  headers: oHeaders,
  mode: 'no-cors',
});

If i try to log the headers of the request object only the accept header will be there.

oReq.headers.forEach(function(v){console.log(v)})
//logs: application/json

If i try to fetch(oReq) i get 401 unauthorized response.

What makes the headers disappear?

Upvotes: 3

Views: 1906

Answers (1)

sideshowbarker
sideshowbarker

Reputation: 88005

When you set mode: 'no-cors'for a request, browsers won’t allow you to set any request headers other than CORS-safelisted request-headers. See the spec requirements:

To append a name/value (name/value) pair to a Headers object (headers), run these steps:

  1. Otherwise, if guard is "request-no-cors" and name/value is not a CORS-safelisted request-header, return.

In that algorithm, return equates to “return without adding that header to the Headers object”.

Upvotes: 3

Related Questions