从 JSON 响应访问 [Symbol(Response internals)]

Accessing [Symbol(Response internals)] from JSON response

我正在使用库 isomorphic-unfetch (https://www.npmjs.com/package/isomorphic-unfetch) 从 Rest API 获取 JSON 数据。这是我提出请求的方式:

    const res = await fetch(
      `url`
    );

要访问 body 我只需要做

    const response = await res.json()

但是如何访问响应 headers?如果我将响应记录到服务器的控制台,这就是我看到的:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    // stuff
  },
  [Symbol(Response internals)]: {
    url: 'request url',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}

什么是 Symbol(Response internals)?我如何访问它的 headers 属性?

要访问其 headers,请使用以下方法之一:

const res = await fetch(url);
console.log(res.headers.get('content-type');
// or
res.headers.forEach(header => console.log(header));

https://github.github.io/fetch/#Headers

当您 运行 遇到这种情况时,您将可以访问响应对象上的那些属性,因此如果您想访问 url 属性你只需要写 response.url 就可以得到你需要的东西。

fetch({someURL}, {
 method: "POST"
}).then((response) => response).then((result) => {return result.url});