最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing [Symbol(Response internals)] from JSON response - Stack Overflow

programmeradmin1浏览0评论

I'm using the library isomorphic-unfetch () to get JSON data from a Rest API. This is how I make the request:

    const res = await fetch(
      `url`
    );

To access the body I simply need to do

    const response = await res.json()

But how do I access the response headers? If I log the response to my server's console this is what I see:

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
  }
}

What's Symbol(Response internals)? And how do I access its headers property?

I'm using the library isomorphic-unfetch (https://www.npmjs.com/package/isomorphic-unfetch) to get JSON data from a Rest API. This is how I make the request:

    const res = await fetch(
      `url`
    );

To access the body I simply need to do

    const response = await res.json()

But how do I access the response headers? If I log the response to my server's console this is what I see:

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
  }
}

What's Symbol(Response internals)? And how do I access its headers property?

Share Improve this question edited Mar 16, 2022 at 9:45 VLAZ 29k9 gold badges62 silver badges83 bronze badges asked Feb 25, 2020 at 11:43 lpetruccilpetrucci 1,6796 gold badges29 silver badges49 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

To access its headers use one of the following:

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

When you run into a situation like this, you'll have access to those properties on the response object, so if you want to get access to the url property you'll simply have to write response.url and you'll get what you need.

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

评论列表(0)

  1. 暂无评论