05 February, 2009

Simulating post-request (using form-data) with file in C/C++

First I started to use WinInet. God, it sucks like most Windows API. I played about a day with http://support.microsoft.com/kb/165298
And I failed to post anything, from what django could make request. Aslo wierdshark shown that MS does a bit strange request (it looses some header about multipart form-data).
Today I took libcurl, it has a rich example who you can perform your POST-request.
Note, that it's just example and you have to define a record and a record_length.

As for me, I took such piece of code from there:


CURL *curl;
CURLcode res;
//
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();


struct curl_httppost* post = NULL;
struct curl_httppost* last = NULL;
struct curl_forms forms[2];
char file1[] = "d:\\backup\\thumb.jpg";

forms[0].option = CURLFORM_FILE;
forms[0].value = file1;
forms[1].option = CURLFORM_END;

curl_formadd(&post, &last, CURLFORM_COPYNAME,
"pictures",
CURLFORM_ARRAY, forms, CURLFORM_END);

res = curl_easy_perform(curl);
curl_easy_cleanup(curl);



Pretty enough. But python is always prettier: snippet
;)

1 comment:

Anonymous said...

POSTER plugin for Firefox is also a good way of simulating POST request:

http://www.quicklyjava.com/simulate-post-request/