cURL / Mailing Lists / curl-library / Single Mail

curl-library

POST request either full of trash or cropped

From: Daniel Spies <daniel.spies_at_fuceekay.com>
Date: Mon, 20 Jul 2009 09:27:03 +0200

Hello list,

I have a small problem I already tried to solve through a forum and until
now nobody really knew an answer. I have a small project automating some
tasks on a website. It kinda works already, but some weird problems still
exist. The major problem is that I cannot create a working POST request on
a special site.

This is the form I try to submit:

<form method='post' name='doaction' action='create.php?open=new'>
<input type='hidden' name='randomval' value='831856300'>
<a href='#'' onCLick='document.doaction.submit()'>Do it now!</a><br /><br
/>

Note: there is really no </form> tag, but I cannot change it. The form
works through a browser, though. And there is no OnSubmit() Event...

Here is one of the two ways I wanted to do this request.

curl_formfree(formpost); // reset everything
curl_easy_reset(curl_handle); // reset everything
curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "cookies.txt");
curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, "cookies.txt");
curl_easy_setopt(curl_handle, CURLOPT_URL,
"http://www.example.com/create.php?open=new");
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.1)");
curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "randomval",
CURLFORM_COPYCONTENTS, rand_val.c_str(), CURLFORM_END);
curl_easy_setopt(curl_handle, CURLOPT_HTTPPOST, formpost);
curl_easy_perform(curl_handle);

Okay, this request does probably not work, because libCurl sends wrong form
data. I checked the packets via Wireshark and it will send data from a
request I did in the beginning of the program. It may happen, that I have
~30 duplicated and invalid fields in my request. No wonder the server is
confused... I was running curl_formfree(formpost); and
curl_easy_reset(curl_handle); before every single request, but it does not
change this behavior. I don't know how I may really get rid of old form
data.

The other try was the following:

curl_easy_reset(curl_handle); // reset everything
curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "cookies.txt");
curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, "cookies.txt");
curl_easy_setopt(curl_handle, CURLOPT_URL,
"http://www.example.com/create.php?open=new");
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.1)");
curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
curl_easy_setopt(curl_handle, CURLOPT_HTTPPOST, 0);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, "randomval=123456789";
curl_easy_perform(curl_handle);

That one gives me a nice tiny packet, but the data is cropped. This is a
packet:

G``E#%@@XPd"A
&POST /login.php HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: www.example.com
Accept: */*
Cookie: sess=1248003067
Content-Length: 17
Content-Type: application/x-www-form-urlencoded

randomval=123456Q

you can see "789" is missing in the randomval variable.
Another example is:

G``EK>@@XPPi
4WPOST /job.php HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: www.example.com
Accept: */*
Cookie: sessid=8523f3b0cd9f9e94972726c69636874; sess=1248006197
Content-Length: 14
Content-Type: application/x-www-form-urlencoded

n/x-www-form-u

Now, can one of you guys explain me what's going on there? "n/x-www-form-u"
is probably not what I want. :D

Any idea? Am I doing something wrong?

Thanks,
Daniel
Received on 2009-07-20