Can't remove default headers. Can't overwrite `"sec-fetch-mode"`, `"connection"` headers.
See original GitHub issue- I can’t remove any default header.
- Setting
"connection": "keep-alive"
throws an error. - I can’t overwrite default
"sec-fetch-mode"
header correctly.
1. I can’t remove the default headers
I can’t remove the default headers: host
, connection
, accept
, accept-encoding
, accept-language
, sec-fetch-mode
, user-agent
.
For example, curl
allows to remove any header (even host
header):
curl http://127.0.0.1:3000 -H 'User-Agent:' -H 'Accept:' -H 'Host:'
And yes, it works.
Using of ""
will send ""
as the header’s value, using of null
will send "null"
as the header’s value.
2. "connection": "keep-alive"
Also, if I manually set connection: "keep-alive"
I get the error (code: 'UND_ERR_INVALID_ARG'
).
const resp = await fetch(url, {
headers: {
"connection": "keep-alive"
}
});
It’s important for code compatibility reason — since node-fetch
uses connection: close
by default.
3. "sec-fetch-mode"
It uses "sec-fetch-mode": "cors"
by default.
As I said above, I can’t remove this header (as well as any other default header).
More over, I can’t change it correctly.
For example, using of:
await fetch(url, {
headers: {
"sec-fetch-mode": "navigate"
}
});
Will produce a request with sec-fetch-mode: navigate, cors
, while it should be sec-fetch-mode: navigate
.
"undici": "4.16.0"
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:25 (16 by maintainers)
You know, there are people out there asking Node to do all these things (like CORS support) and some server runtimes like Deno with the concept of an origin are in fact implementing some of this stuff.
So this is not as out of the question as you may assume.
Also, please avoid sarcasm/snark in the tracker. We don’t know you well (yet) and it’s super easy to take things the wrong way in these situations without a lot of prior context (which we don’t have yet!) and it’s important to me that your contribution (feedback) on what node does here doesn’t get overlooked because of that.
That said I tend to agree node’s fetch should allow overriding these things unlike the browser’s. I also absolutely agree with Robert it’s not clear cut. I can ask in a forum of server runtimes I participate in but if you want to move this ahead it’d be great to do what server runtimes that implement fetch do (Deno/cloudflare/shopify etc) as well as what user-land implementations (like node-fetch) do.
Easier said than done. Knowing what parts of the spec to ignore or not is not that easy. To keep things simple with the limited development time we have (I’m doing this work for free) it’s much easier to just follow the spec verbatim, i.e. the aim here is to match the browser as closely as possible and not bike-shed over what is right or wrong; the spec/Chrome is right (with few well discussed exceptions).
fetch is a browser API. If you don’t want/need the browser behavior then don’t use the fetch API. We have much better API’s for node, e.g.
undici.request
.