'modify the post data of a request in firefox extension

I am trying to trap an http request, change some of its post parameters and send the modified request. I tried using the setData method of upload stream to modify the request, but the same original request is sent.

I have the following code execute on the "http-on-modify-request" :

//rewind the request to read post body  
channel= subject.QueryInterface(Components.interfaces.nsIHttpChannel);
channel=channel.QueryInterface(Components.interfaces.nsIUploadChannel);  
channel = channel.uploadStream;  
channel.QueryInterface(Components.interfaces.nsISeekableStream)
                .seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);  
var stream = Components.classes["@mozilla.org/binaryinputstream;1"]
                .createInstance(Components.interfaces.nsIBinaryInputStream);  
stream.setInputStream(channel);  
var postBytes = stream.readByteArray(stream.available());  
poststr = String.fromCharCode.apply(null, postBytes);  

//change the poststr

poststr=poststr.replace(....);  
stringStream.setData(poststr, poststr.length);  
//changing the postdata  
channel = channel.QueryInterface(Components.interfaces.nsIUploadChannel);  
channel = channel.uploadStream;  
channel = channel.QueryInterface(Components.interfaces.nsISeekableStream)
          .seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);  
channel.uploadStream.QueryInterface(Components.interfaces.nsIMIMEInputStream);  
channel.uploadStream.setData(stringStream);  
channel.send();

What am I doing wrong here? I tried aborting the initial request and starting with a fresh request, but then the page doesn't load at all. Thanx in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source