Search code examples
javascriptfirefoxfirefox-addon

How to send a POST request from a Firefox extension?


I would like to send a POST request to a web server from a firefox extension.

I have found this example to send POST requests; https://developer.mozilla.org/en/Creating_Sandboxed_HTTP_Connections#HTTP_notifications

but I can't get it to work.

I currently have code like this;

    var ioService = Components.classes["@mozilla.org/network/io-service;1"]
                              .getService(Components.interfaces.nsIIOService);
var uri = ioService.newURI("http://www.google.com", null, null);
gChannel = ioService.newChannelFromURI(uri);

postData = "a=1&b=2&c=3";

var inputStream = Components.classes["@mozilla.org/io/string-input-stream;1"]
    .createInstance(Components.interfaces.nsIStringInputStream);

inputStream.setData(postData, postData.length);

var uploadChannel = gChannel.QueryInterface(Components.interfaces.nsIUploadChannel);

uploadChannel.setUploadStream(inputStream, "application/x-www-form-urlencoded", -1);
uploadChannel.requestMethod = "POST";
uploadChannel.open();

but I get an error about "cant modify properties of a WrappedNative"


Solution

  • How about using XMLHttpRequest object. There is no same origin policy in extension development