Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get Headers #183

Open
auedbaki opened this issue Jun 27, 2021 · 2 comments
Open

Unable to get Headers #183

auedbaki opened this issue Jun 27, 2021 · 2 comments

Comments

@auedbaki
Copy link

auedbaki commented Jun 27, 2021

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.

@dennisfrijlink
Copy link

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

@dennisfrijlink
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants