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.
hey I figured out what was wrong!! :)
The uploadstream.setData has a bud. It sets the request method of the channel to PUT as opposed to POST So we need to change that after the setData call. The following seems to solve the problem :)
@ MatrixFrog : You are right. I don't need to call
channel.send
. That part is taken care of :)