我正在使用库isomorphic-unfetch
(https://www.npmjs.com/package/isomorphic-unfetch)从Rest API获取JSON数据。这就是我提出请求的方式:
const res = await fetch(
`url`
);
要访问身体,我只需要进行
const response = await res.json()
但是我该如何访问响应标头呢?如果我将响应记录到我的服务器控制台,这就是我所看到的:
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});