By visiting our site, you agree to our privacy policy regarding cookies, tracking statistics, etc.

proper noun logo blue
  • Content Marketing
    • SEO Articles
    • Pillar Content
    • Website Content
    • Content Briefs
  • Off-Page
    • Managed Outreach
  • Optimization
    • Content Optimization
    • Technical SEO
  • Verticals
    • Ecommerce SEO Services
    • SaaS SEO Services
    • Enterprise Business SEO
    • Local SEO Services
  • Case Studies
  • Support
The Roadmap
Book A Demo
How To Get Response Headers on A Fetch Request
January 27, 2022by adamWeb Development

How To Get Response Headers on A Fetch Request

When dealing with requests using Javascript, for example with the Fetch API, you may find yourself needing to deal with response headers.

Many APIs will use header information to share useful data. The WordPress API returns information like the total number of posts in a post type and the total number of pages at the current pagination sizing.

Unlike a typical JSON response, the headers aren’t immediately available on the response, which may trip you up if you’re new to the Fetch API. Let’s say your Fetch request looks like this:


fetch('//www.website.com/wp-json/wp/v2/posts?per_page=10&page=1')
.then(res => {
// do something
})

In order to access the response header ‘X-WP-TotalPages’ you would handle the response like this:


fetch('//www.website.com/wp-json/wp/v2/posts?per_page=10&page=1')
.then(res => {
// do something
totalPages = parseFloat(res.headers.get('X-WP-TotalPages'))
})

Instead of trying to access the name of the header right on the “headers” object, you have to use the get function to pull the header data.

This can be used in a variety of ways, for example making requests to any number of API’s in frontend react applications.

You can read more about request and response headers at Mozilla.org

Other Good Resources

  • Information About the Fetch API
  • How to use Javascript Fetch API
  • Fetch API & React Examples
  • Running Content Marketing Campaigns
Read More
Share
proper noun logo blue

Proper Noun is an SEO, digital growth, and content marketing agency located in Fort Lauderdale, FL.

Twitter Facebook-f Google-plus-g Pinterest-p Linkedin-in

Contacts

Our Offices
300 SW 1st Ave #155
Fort Lauderdale, FL 33301
Give Us A Call
(954)674-1258

Our Services

  • Content Marketing
  • Ecommerce SEO Services
  • Enterprise Business SEO
  • Managed Outreach
  • Technical SEO
  • Website Optimization

Our Company

  • Blog
  • Contact Us
  • Book A Demo
  • Case Studies
  • Article Writing
  • Website Directory

© 2022 Proper Noun. All Rights Reserved.

Proper Noun is a content marketing agency located in Fort Lauderdale, FL. Proper Noun provides managed content marketing campaigns and managed outreach campaigns for ecommerce businesses, saas products, law firms, real estate, enterprise businesses and much more. We also offer a la carte content creation services.

Markets We Serve

  • Ecommerce SEO
  • Enterprise SEO
  • International SEO
  • National SEO
  • Local SEO
  • B2B SEO
  • Whitelabel SEO
  • SEO Consulting
  • SEO Reseller

Contacts



8 800 2563 123

  emil@yoursite.com

 27 Division St, New York, NY 10002, United States