简体   繁体   English

告诉 fetch() 根本不发送 Content-Type header

[英]Tell fetch() to not send a Content-Type header at all

Is there a way to send an HTTP request with fetch() and tell it to not automatically set a header?有没有办法使用fetch()发送 HTTP 请求并告诉它不要自动设置 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的请求,我可以让它发送一个空的 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?但我可以让它根本不包括这条线吗?

Per step 36 of https://fetch.spec.whatwg.org/#dom-request and https://fetch.spec.whatwg.org/#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:根据https://fetch.spec.whatwg.org/#dom-requesthttps://fetch.spec.whatwg.org/#concept-bodyinit-extract的第 36 步,带有请求主体的fetch发起的请求将总是以Content-Type header 结尾——因为在规范中,带有请求主体的请求只能采用两种可能的路径:

  1. If you don't specify any Content-Type header at all in your fetch call, then the browser always automatically on its own sets a Content-Type header for the request;如果您在fetch调用中根本没有指定任何Content-Type header,那么浏览器总是会自动为请求设置Content-Type header; unless…除非…
  2. If you specify a Content-Type header in your fetch call, then the browser uses the value you set.如果您在fetch调用中指定Content-Type header,则浏览器将使用您设置的值。

So because those two are the only possible outcomes and because the fetch API doesn't provide any method for completely 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.因此,因为这两个是唯一可能的结果,并且因为fetch API 没有提供任何方法来完全删除任何浏览器设置的请求标头,所以如果请求具有请求正文,则无法使用fetch API 发送请求没有它以Content-Type header 结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM