Skip to content

Instantly share code, notes, and snippets.

@perlmonk
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perlmonk/1f1d087fcf1dadbafd16 to your computer and use it in GitHub Desktop.
Save perlmonk/1f1d087fcf1dadbafd16 to your computer and use it in GitHub Desktop.
test curl 'curl_formadd' submit array field
// http://curl.haxx.se/libcurl/c/postit2.html
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
CURLFORMcode add_result;
curl_global_init(CURL_GLOBAL_ALL);
/* Fill in the file upload field */
add_result = curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file[]",
CURLFORM_FILE, "form_test.cpp",
CURLFORM_END);
if (add_result != 0) {
printf("curl_formadd: %d\n", add_result);
}
/* Fill in the filename field */
add_result = curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file[]",
CURLFORM_COPYCONTENTS, "form_test.cpp",
CURLFORM_END);
if (add_result != 0) {
printf("curl_formadd: %d\n", add_result);
}
curl = curl_easy_init();
if(curl) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, "http://aaba.me/cgi-bin/t.cgi");
// set form
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
// debug proxy
curl_easy_setopt(curl, CURLOPT_PROXY, "http://127.0.0.1");
curl_easy_setopt(curl, CURLOPT_PROXYPORT, 8888);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
printf("curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment