Is there a way to send an HTTP request with fetch()
and tell it to not automatically set a header? I'm trying to get it to send a request without a Content-Type
, I can get it to send an empty header
Content-Type:
like this
fetch('http://localhost:8000/', {
method: 'POST',
headers: {
'Content-Type': ''
},
body: 'some data'
});
but can I get it to not include the line at all?
Is there a way to send an HTTP request with fetch()
and tell it to not automatically set a header? I'm trying to get it to send a request without a Content-Type
, I can get it to send an empty header
Content-Type:
like this
fetch('http://localhost:8000/', {
method: 'POST',
headers: {
'Content-Type': ''
},
body: 'some data'
});
but can I get it to not include the line at all?
Share Improve this question edited Aug 29, 2022 at 11:57 asked Mar 30, 2022 at 0:27 user3064538user3064538 01 Answer
Reset to default 8Per step 36 of https://fetch.spec.whatwg/#dom-request and https://fetch.spec.whatwg/#concept-bodyinit-extract, a fetch
-initiated request with a request body will always end up with a Content-Type
header — because there are only two possible paths a request with a request body can take in the spec:
- If you don’t specify any
Content-Type
header at all in yourfetch
call, then the browser always automatically on its own sets aContent-Type
header for the request; unless… - If you specify a
Content-Type
header in yourfetch
call, then the browser uses the value you set.
So because those two are the only possible outes and because the fetch
API doesn’t provide any method for pletely removing any browser-set request headers, then if a request has a request body, there’s no way to send the request using the fetch
API without it ending up with a Content-Type
header.