Chrome API responseHeaders

google-chrome, google-chrome-extension

Solution

You have to request the response headers like this:

chrome.webRequest.onHeadersReceived.addListener(function(details){
  console.log(details.responseHeaders);
},
{urls: ["http://*/*"]},["responseHeaders"]);

An example of use. This is one instance of how I use the `webRequest` api in my extension. (Only showing partial incomplete code)

I need to indirectly access some server data and I do that by making use of a 302 redirect page. I send a `Head` request to the desired url like this:

$.ajax({
  url: url,
  type: "HEAD"
  success: function(data,status,jqXHR){
    //If this was not a HEAD request, `data` would contain the response
    //But in my case all I need are the headers so `data` is empty
    comparePosts(jqXHR.getResponseHeader('redirUrl')); //where I handle the data
  }     
});

And then I silently kill the redirect while scraping the `location` header for my own uses using the `webRequest` api:

chrome.webRequest.onHeadersReceived.addListener(function(details){
  if(details.method == "HEAD"){
    var redirUrl;
    details.responseHeaders.forEach(function(v,i,a){
      if(v.name == "Location"){
       redirUrl = v.value;
       details.responseHeaders.splice(i,1);
      }
    });
    details.responseHeaders.push({name:"redirUrl",value:redirUrl});
    return {responseHeaders:details.responseHeaders}; //I kill the redirect
  }
},
{urls: ["http://*/*"]},["responseHeaders","blocking"]);

I actually handle the data inside the `onHeadersReceived` listener, but this way shows where the response data would be.

Problem

Based on this documentation: https://developer.chrome.com/extensions/webRequest.html#event-onHeadersReceived I tried to display the response via the console like: `console.log(info.responseHeaders);` But its returning `undefined`. But this works though: `console.log("Type: " + info.type);` Please help, I really need to get the responseHeaders data.

Original source