Posting data with GM_xmlhttprequest

1,341 views
Skip to first unread message

Juan Pablo Guereca Alonso

unread,
Oct 26, 2006, 10:41:56 AM10/26/06
to greasemon...@googlegroups.com
Hey:

Has anyone posted data with GM_xmlhttprequest using method POST??

I'm not able to pass data with this code, the web is loaded but no data
is passed, I've sniffed the transmission...

any idea, could it be a greasemonkey bug??

var submit = function(params) {
GM_xmlhttpRequest ( {
method : "POST",
url : url,
headers : {
'content-type': 'application/x-www-form-urlencoded',
'content-length': params.length,
'User-agent': 'Mozilla/4.0 (compatible)',
'Accept': '*'
},
data : params,
onload : function(details)
{
if (details.readyState == 4) {
if (details.status != 200) {
return;
}
alert(details.responseText);
}
}
} );
}

Edward Hibbert

unread,
Oct 26, 2006, 10:49:04 AM10/26/06
to greasemon...@googlegroups.com
I had problems with this, and instead opted to use GET to post data,
passing parameters in the URL separated by &.

Dave Nelson

unread,
Oct 26, 2006, 11:00:35 AM10/26/06
to greasemon...@googlegroups.com
On 10/26/06, Juan Pablo Guereca Alonso <jpgu...@gmail.com> wrote:
[...]

> var submit = function(params) {
> GM_xmlhttpRequest ( {
> method : "POST",
> url : url,
[...]

Am I missing something, or is 'url' undefined? It seems like it should
be a parameter to your function or something, like:
var submit = function(url, params) {

I don't know why this would happen, though: "the web is loaded but no
data is passed", because it seems like the opposite is being set up...
creating a request that has no URL but does have parameters.

--
-dave

geek blog: http://spugbrap.blogspot.com
personal blog: http://spugbrap-personal.blogspot.com
new (consolidated) blog: http://spugbrap.wordpress.com
family website: http://www.oatmealcookies.org
bookmarks: http://del.icio.us/spugbrap

Juan Pablo Guereca Alonso

unread,
Oct 26, 2006, 11:07:29 AM10/26/06
to greasemon...@googlegroups.com
Vars url and params are defined and are global, the request is done but
the body has nothing.

Arvid Jakobsson

unread,
Oct 26, 2006, 12:13:07 PM10/26/06
to greasemon...@googlegroups.com
Try it out using the form on the website. Sniff the request with something like burpproxy. Then sniff the data your script posts. Check what differs.

Juan Pablo Guereca Alonso

unread,
Oct 26, 2006, 12:35:44 PM10/26/06
to greasemon...@googlegroups.com
I dit it and the difference is that gm_xmlhttprequest doesn't send any
data on the body.

Nobody has ever post data with gm_xmlhttprequest??

Jonas Lundberg

unread,
Oct 26, 2006, 12:43:00 PM10/26/06
to greasemon...@googlegroups.com
Here is an example using multipart-form-data for you (not exactly what
you wanted, but quite close maybe?) . The data is formatted using the
postdata() function. In my example, it changes a normal form on a page
into an ajax-call, so you have to alter that into what you want to do.
Note that it also uses encodeURIComponent to get around that awful
text encoding bug that plagues Greasemonkey/Firefox.

function send(data) {
GM_xmlhttpRequest({
method: 'POST',
url: mysubmit,
headers: {
'Content-type': 'multipart/form-data; boundary=shapecms',
},
data: data,
onload: function(responseDetails) {
var ok = '200';
if(responseDetails.status == ok) {
}
else {
GM_log('error message:'+responseDetails.responseText);
}
GM_log(responseDetails.responseText);
}
}) };

function postdata() {
var newseparator = '--'+separator;
var submitFormInput = xpath("//form[@id='shapecms_web2']//input");
var submitFormTextarea =
xpath("//form[@id='shapecms_web2']//textarea");
var postdata = postInput(submitFormInput, newseparator);
if(postdata != "") postdata += '\r\n'+
postInput(submitFormTextarea, newseparator);
else postdata += postInput(submitFormTextarea, newseparator);
if(postdata) postdata +='\r\n'+newseparator+'--'
return(postdata);
}

function postInput(form, separator) {
var postdata ="";
var newline = '\r\n';

for(var i = 0; i < form.snapshotLength; i++) {
var field = form.snapshotItem(i);
// GM_log('Field:'+field.tagName);
// GM_log(field.name + "=" + field.value);
if(postdata !="") postdata += newline;
var tosend = encodeURIComponent(field.value);
postdata += separator+newline+'Content-Disposition:
form-data; name="'+field.name+'"'+newline+newline+tosend;
}
return(postdata);
}

[snip]

Hans

Jonas Lundberg

unread,
Oct 26, 2006, 12:46:02 PM10/26/06
to greasemon...@googlegroups.com
Sorry, I forgot one line:
var separator ="shapecms";


On 10/26/06, Jonas Lundberg <my.name...@gmail.com> wrote:
> Here is an example using multipart-form-data for you (not exactly what
> you wanted, but quite close maybe?) . The data is formatted using the

[snip]

Hans
>

Philip Friedman

unread,
Oct 26, 2006, 2:35:22 PM10/26/06
to greasemon...@googlegroups.com
I looked at my use of GM_xmlhttpRequest( { method:'POST', works fine on several sites. I used the Tamper Data extension to capture the real posts and copied all the headers and data from there. I'd  start by hard coding a set of values from Tamper Data, then when that works, start using the values from parameters and variables.

Regards, Phil Friedman

Juan Pablo Guereca Alonso

unread,
Oct 26, 2006, 2:58:24 PM10/26/06
to greasemon...@googlegroups.com
I've fixed the problem:

- I have removed on the header 'content-length': params.length.
- The params are global so I also have removed the param argument.

Thanks anyway... ;)

Reply all
Reply to author
Forward
0 new messages