node.js 使用Fetch API时如何设置请求头的内容类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38156239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:22:02  来源:igfitidea点击:

How to set the content-type of request header when using Fetch APi

node.jsrequest-headersfetch-apinodeapiisomorphic-fetch-api

提问by user1526912

I am using npm 'isomorphic-fetch' to send requests. The problem I am experiencing is I am unable to set the content-type of the request header.

我正在使用 npm 'isomorphic-fetch' 发送请求。我遇到的问题是我无法设置请求标头的内容类型。

I set a content type of application/json , however the request header are being set to text/plain.

我将内容类型设置为 application/json ,但是请求标头被设置为 text/plain。

import 'isomorphic-fetch';

  sendRequest(url, method, body) {
    const options = {
      method: method,
      headers:{'content-type': 'application/json'},
      mode: 'no-cors'
    };

    options.body = JSON.stringify(body);

    return fetch(url, options);
  }

When I examine the request in my browser the content type is o :

当我在浏览器中检查请求时,内容类型为 o :

content-type:text/plain;charset=UTF-8

Can anyone explain why I am unable to set this property?

谁能解释为什么我无法设置此属性?

回答by RTS

You need to create a fetch headers object.

您需要创建一个 fetch headers 对象。

sendRequest(url, method, body) {
    const options = {
        method: method,
        headers: new Headers({'content-type': 'application/json'}),
        mode: 'no-cors'
    };

    options.body = JSON.stringify(body);

    return fetch(url, options);
}

回答by user1526912

I found the answer after reading the following article:

我在阅读以下文章后找到了答案:

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Headers

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Headers

Guard

Since headers can be sent in requests and received in responses, and have various limitations about what information can and should be mutable, headers objects have a guard property. This is not exposed to the Web, but it affects which mutation operations are allowed on the headers object.

Possible guard values are:

  • none: default.
  • request: guard for a headers object obtained from a request (Request.headers).
  • request-no-cors: guard for a headers object obtained from a request created with Request.modeno-cors.
  • response: guard for a Headers obtained from a response (Response.headers).
  • immutable: Mostly used for ServiceWorkers; renders a headers object read-only.

Note: You may not append or set a requestguarded Headers' Content-Lengthheader. Similarly, inserting Set-Cookieinto a response header is not allowed: ServiceWorkers are not allowed to set cookies via synthesized responses.

警卫

由于标头可以在请求中发送并在响应中接收,并且对于哪些信息可以和应该是可变的有各种限制,因此标头对象具有保护属性。这不会暴露给 Web,但它会影响 headers 对象上允许进行哪些更改操作。

可能的保护值是:

  • none: 默认。
  • request: 保护从请求 ( Request.headers) 中获得的标头对象。
  • request-no-cors: 保护从使用创建的请求获得的标头对象Request.modeno-cors
  • response: 保护从响应 ( Response.headers) 中获得的 Headers 。
  • immutable: 主要用于 ServiceWorkers;将 headers 对象呈现为只读。

注意:您不能附加或设置受request保护的 HeadersContent-Length标头。同样,Set-Cookie不允许插入响应头:ServiceWorker 不允许通过合成响应设置 cookie。

When the options mode property is set to no-cors the request header values are immutable.

When the options mode property is set to no-cors the request header values are immutable.

Instead I set the mode property to cors.

相反,我将模式属性设置为 cors。