lquixada / cross-fetch

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

Inconsistent behavior for headers with native Fetch API and whatwg-fetch #151

Closed loderunner closed 1 year ago

loderunner commented 1 year ago

According to the Fetch documentation on MDN, the headers property of the options parameter can be:

a Headers object or an object literal with String values.

However, according to an example for errors below, an array of arrays of strings can be accepted (provided the inner arrays are of length 2).

Invalid header value. The header object must contain exactly two elements.

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });

This header initializer option is supported by the Headers constructor parameter, which can be

a simple object literal with String values, an array of name-value pairs, where each pair is a 2-element string array; or an existing Headers object.


Testing in Chrome 109.0.5414.87, I found an inconsistency between what is supported by cross-fetch, whatwg-fetch and what is supported by Chrome's native Fetch API.

I found the same results for:


My test bench for Headers support is here.

StaphSynth commented 1 year ago

Thank you for investigating this, it really helped us while tracking down a header issue in one of our apps.

lquixada commented 1 year ago

Version 4 has been officially released with whatwg-fetch@3.6.2. Please check it out: npm install cross-fetch.