Inconsistent behavior for headers with native Fetch API and whatwg-fetch
loderunner opened this issue · comments
According to the Fetch documentation on MDN, the headers
property of the options
parameter can be:
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.
- Chrome Fetch:
- Object
- Array
- Chrome Fetch Headers class
- whatwg-fetch Headers class
- cross-fetch Headers class
whatwg-fetch
:- Object
- whatwg-fetch Headers class
cross-fetch
:- Object
- Array
- cross-fetch Headers class
I found the same results for:
- Safari 16.0 (17614.1.25.9.10, 17614)
- Firefox 108.0.2
My test bench for Headers support is here.
Thank you for investigating this, it really helped us while tracking down a header issue in one of our apps.
Version 4 has been officially released with whatwg-fetch@3.6.2
. Please check it out: npm install cross-fetch
.