1

I am trying to get the HTTP response by reading the response header. But I am not able to retrieve anything. What I am missing exactly? Here is my code in index.js

var {Cc, Ci} = require("chrome");
var httpRequestObserver =
{
   init: function() {
      var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
      observerService.addObserver(this, "http-on-examine-response", false);
   },

  observe: function(subject, topic, data) 
  {
    if (topic == "http-on-examine-response") {
         subject.QueryInterface(Ci.nsIHttpChannel);
            this.onExamineResponse(subject);
    }
  },
onExamineResponse: function (oHttp)
  {  
        console.log("Header :"+JSON.stringify(oHttp));
        try
       {
         var header_value = oHttp.getResponseHeader("<the_header_that_i_need>"); 
         // URI is the nsIURI of the response you're looking at 
         // and spec gives you the full URL string
         var url = oHttp.URI.spec;
       }
       catch(err)
       {
         console.log(err);
       }
   }
};

httpRequestObserver.init();

I get reference from this stackoverflow and several online blogs.:- Firefox add-on SDK: Get http response headers

Edited

I Checked for the values in console.

  • subject is coming as ()
  • data is coming as null
Community
  • 1
  • 1
vineetv2821993
  • 927
  • 12
  • 25

1 Answers1

0

Do:

 oHttp = oHttp.QueryInterface(Ci.nsIHttpChannel);

in onExaminResponse then after this getRepsonseHeader should work

edit:

this works for me:

Services.obs.removeObserver(httpRequestObserver, "http-on-examine-response", false);
var httpRequestObserver =
{
   init: function() {
      Services.obs.addObserver(this, "http-on-examine-response", false);
   },

  observe: function(subject, topic, data) 
  {
         subject = subject.QueryInterface(Ci.nsIHttpChannel);
            this.onExamineResponse(subject);
  },
onExamineResponse: function (oHttp)
  {  
        //console.log("Header :"+JSON.stringify(oHttp));
         var url = oHttp.URI.spec;
         console.log('url:', url);

        try
       {
         // URI is the nsIURI of the response you're looking at 
         // and spec gives you the full URL string
         var header_value = oHttp.getResponseHeader("X-Firefox-Spdy"); 
         console.log('header_value:', header_value);
       }
       catch(err)
       {
         if (err.name == 'NS_ERROR_NOT_AVAILABLE') {
           console.log('this http request does not have header X-Firefox-Spdy')
         } else {
          console.error(err);
         }
       }
   }
};

httpRequestObserver.init();
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • Sorry but it is still not working. I also tried "subject = subject.QueryInterface(Ci.nsIHttpChannel);" but that is not working too :( – vineetv2821993 Sep 29 '15 at 22:36
  • @vineetv2821993 `subject = subject.QueryInterface(Ci.nsIHttpChannel);` you have to set it equals – Noitidart Sep 29 '15 at 23:03
  • 1
    Hi @Noitidart I done silly mistake. console.log("Header :"+JSON.stringify(oHttp)); I dont know why JSON.stringify is making an interruption but it is sure now it is the problem. Is it some kind of bug? – vineetv2821993 Sep 29 '15 at 23:25
  • I'm not sure, that's interesting if that's true though. Can you verify for sure please. – Noitidart Sep 30 '15 at 00:13
  • I check that in other code. JSON.stringify is not interrupting but in this code. Can you try this to make sure I am right? Uncomment the line and test. – vineetv2821993 Sep 30 '15 at 13:09
  • @vineetv Ah weird I think it is. I am getting `TypeError: cyclic object value` on the line after the `JSON.stringify` very weird. Can you please file this on http://bugzilla.mozilla.org/ – Noitidart Sep 30 '15 at 14:02