http icon indicating copy to clipboard operation
http copied to clipboard

Unable to get Headers

Open auedbaki opened this issue 4 years ago • 2 comments

I'm requesting data from Wordpress Api, which serves some data in response headers:

X-WP-Total: the total number of records in the collection
X-WP-TotalPages: the total number of pages encompassing all available records

But on Logging response get empty object. see below image Screenshot 2021-06-27 at 8 23 10 PM

But in network request getting headers. see below image Screenshot 2021-06-27 at 8 25 36 PM

Please resolve this issue, if not already or Guide me with the solution.

auedbaki avatar Jun 27 '21 14:06 auedbaki

Facing the same issue. Also using wordpress but the headers are empty.

Kowalski127 avatar Jul 21 '21 12:07 Kowalski127

After searching for a solution I found this: The Headers class instance that fetch returns is an iterable, as opposed to a plain object like axios returns. Some iterable's data, such as Headers or URLSearchParams, aren't viewable from the console, you have to iterate it and console.log each element, like:

fetch('http://localhost:9876/test/sample-download', {
    method: 'post',
    headers: {},
    body: {}
})
.then(response => {
  // Inspect the headers in the response
  response.headers.forEach(console.log);
  // OR you can do this
  for(let entry of response.headers.entries()) {
    console.log(entry);
  }
})
.catch(err => console.error(err));

Resource: https://stackoverflow.com/questions/48413050/missing-headers-in-fetch-response

So in Nuxt you can use:

const res = await this.$http.get('https://localhost:3000/wordpress/wp-json/wc/v3/products/')
res.headers.forEach(console.log)

Kowalski127 avatar Jul 21 '21 13:07 Kowalski127