javascript - Getting HTTP Response Header in Firefox Addon -
i trying http response reading response header. not able retrieve anything. missing exactly? here 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 nsiuri of response you're looking @ // , spec gives full url string var url = ohttp.uri.spec; } catch(err) { console.log(err); } } }; httprequestobserver.init();
i reference stackoverflow , several online blogs.:- firefox add-on sdk: http response headers
edited
i checked values in console.
- subject coming ()
- data coming null
do:
ohttp = ohttp.queryinterface(ci.nsihttpchannel);
in onexaminresponse
after getrepsonseheader
should work
edit:
this works 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 nsiuri of response you're looking @ // , spec gives 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 not have header x-firefox-spdy') } else { console.error(err); } } } }; httprequestobserver.init();
Comments
Post a Comment