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.
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.