Tuesday, July 24, 2007

xul Post method

I just found this guide this morning..how to do a "post" (not a get httpRequest) in xml.
hope it is also useful to all of you.

Preprocessing POST data
var dataString = "name1=data1&name2=data2";
// POST method requests must wrap the encoded text in a MIME
// stream
const Cc = Components.classes;
const Ci = Components.interfaces;
var stringStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
if ("data" in stringStream) // Gecko 1.9 or newer
stringStream.data = dataString;
else // 1.8 or older
stringStream.setData(dataString, dataString.length);
var postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
postData.addContentLength = true;
postData.setData(stringStream);

// postData is ready to be used as aPostData argument

No comments: