Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get headers of the response from fetch

I am using fetch on chrome Version 52.0.2743.82 (64-bit). I want to get all the headers in the response. Following snippet only return content-type but if you peek into chrome dev tools it shows many other response headers. How to get other headers from fetch.

  fetch('https://httpbin.org/get')
    .then(response => {
       const headers = response.headers.entries();
       let header = headers.next();
       while (!header.done){
         console.log(headers.value);
         header = header.next();
       }
    })

I tried polyfilling(manually overriding) the github implementation. Still no luck.

like image 235
udnisap Avatar asked Aug 15 '16 11:08

udnisap


People also ask

How do I get response headers using Fetch?

We then fetch this request using fetch() , extract a blob from the response using Response. blob , create an object URL out of it using URL. createObjectURL , and display this in an <img> . Note that at the top of the fetch() block, we log the response headers to the console.

How do I get response header responses?

View headers with browser development toolsSelect the Network tab. You may need to refresh to view all of the requests made for the page. Select a request to view the corresponding response headers.

What is headers in Fetch?

The Headers interface of the Fetch API allows you to perform various actions on HTTP request and response headers. These actions include retrieving, setting, adding to, and removing headers from the list of the request's headers.

How do you get the curl response header?

We can use curl -v or curl -verbose to display the request headers and response headers in the cURL command. The > lines are request headers . The < lines are response headers .

How do I capture a request header?

You will need to create a Request Attribute for the request headers you want to capture in Settings -> Server-side service monitoring -> Request Attribute. You will have to specify the request attributes you want to capture in the configuration.


1 Answers

You can't access all the headers when requesting cross-domain content via ajax. You can access all headers if origin is same.

As explained in W3 Specifications here, only Content-Type, Last-modified, Content-Language, Cache-Control, Expires, Pragma headers are accessible.

Further https://httpbin.org/get send only Content-Type header from list of accessible headers so, you got that only.

Edit: You can expose non-standard CORS response headers by sending Access-Control-Expose-Headers with the headers you want the client to access on the response.

like image 68
Adnan Umer Avatar answered Oct 17 '22 10:10

Adnan Umer