lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.67k stars 104 forks source link

Custom X-Auth-Token is missing #54

Closed edouard-lopez closed 5 years ago

edouard-lopez commented 5 years ago

I send a fetch requesh follow:

const payload = computePayload(options, token, tenant);
const response = await fetch(`https://${getServer(host, port)}/${endpoint}`, payload);

When I use isomorphic-fetch it works as excpected, but when I switch to cross-fetch this fails.

:x: Using cross-fetch and Headers() fails

Where computePayload() create a headers oject like:

options.headers = new Headers({
  Accept: 'application/json',
  'Content-Type': 'application/json',
  'X-Auth-token': token,
});

The resulting request is:

fetch('https://wazo/api/confd/1.0/resellers/f1e32efc-7d6b-4797-879e-7dfa03aa3023', {
  credentials: 'omit',
  referrer: 'http://localhost:3000/',
  referrerPolicy: 'no-referrer-when-downgrade',
  body: null,
  method: 'GET',
  mode: 'cors',
});

headers is missing :/

:heavy_check_mark: Using cross-fetch and object literal

A workaround is to use a literal object instead of Headers():

options.headers = {
  Accept: 'application/json',
  'Content-Type': 'application/json',
  'X-Auth-Token': token,
};

The resulting request is:

fetch('https://wazo/api/confd/1.0/resellers/f1e32efc-7d6b-4797-879e-7dfa03aa3023', {
  credentials: 'omit',
  headers: {
    accept: 'application/json',
    'content-type': 'application/json',
    'x-auth-token': 'e338eb-d66a-4f1f-87bd-22ab9834e8b',
  },
  referrer: 'http://localhost:3000/',
  referrerPolicy: 'no-referrer-when-downgrade',
  body: null,
  method: 'GET',
  mode: 'cors',
});

Question

Why is cross-fetch not working with Headers()?

lquixada commented 5 years ago

it's probably some implementation issue with the computePayload function. isomorphic-fetch is pretty old, so that function might be interacting with the Headers api in a way that is not supported anymore or the lib has been monkey patched somehow. Headers does not have any special handle for the X-Auth-Token header.