title: Not able to modify POST data in Firefox extension
body: <p>I am trying to implement a Firefox Extension which modify the POST request data. </p>
  
  <p>Code follows, it fails where marked "<em>Fails here!!!</em>"</p>
  
  <p>Any insight would be helpful.</p>
  
  <pre><code>    Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  
      var newData = "test 123";
      function LOG(msg) {
          var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                       .getService(Components.interfaces.nsIConsoleService);
          consoleService.logStringMessage(msg);
      }
  
      function CMP() {
          this.registered = false;
          this.register();
      }
  
      CMP.prototype = {
  
          register: function() {
              if (this.registered == false) {
                  var observerService = Components.classes["@mozilla.org/observer-service;1"]
                      .getService(Components.interfaces.nsIObserverService);
                  observerService.addObserver(this, "http-on-modify-request", false);
                  this.registered = true;
              }
          },
  
          observe: function(subject, topic, data) 
          {
              LOG("Inside observe");
              if (topic == "http-on-modify-request") 
              {
                  LOG("TOPIC is http-on-modify-request");
                  var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
  
                  if(httpChannel.requestMethod == "POST"){
                      LOG("Inside POST")
                      var uploadChannel = httpChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
                      //var uploadChannelStream = uploadChannel.uploadStream; 
  </code></pre>
  
  <p>Modify the data here. Here for testing i am passing "test 123" as new data</p>
  
  <pre><code>                    var newStringInputStream = Components.classes['@mozilla.org/io/string-input-stream;1'].createInstance(Components.interfaces.nsIStringInputStream);
                      newStringInputStream.setData(newData,newData.length);
                      LOG("set data in newStringInputStream!!");
  
                      uploadChannel.setUploadStream(newStringInputStream, "text/plain", -1 );// Fails here!!!
                      httpChannel.requestMethod = "POST";
                      LOG("upload DONE!!")  
                  }   
              }
          },
  
          QueryInterface : function(aIID) {
          if (aIID.equals(Components.interfaces.nsISupports) ||
              aIID.equals(Components.interfaces.nsIObserver))
            return this;
          throw Components.results.NS_NOINTERFACE;
          },
  
          unregister: function() {
              var observerService = Components.classes["@mozilla.org/observer-service;1"]
                  .getService(Components.interfaces.nsIObserverService);
              observerService.removeObserver(this, "http-on-modify-request");
          },
  
          classID: Components.ID('{F799F47E-ABA5-4AF1-B8F2-BD74E3E5BCC0}'),
          QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIObserver])
      };
  
      if (XPCOMUtils.generateNSGetFactory)
      {
        var NSGetFactory = XPCOMUtils.generateNSGetFactory([CMP]);
      }
  </code></pre>