1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
9 * Example code that uploads a file name 'foo' to a remote script that accepts
10 * "HTML form based" (as described in RFC1738) uploads using HTTP POST.
12 * The imaginary form we'll fill in looks like:
14 * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
15 * Enter file: <input type="file" name="sendfile" size="40">
16 * Enter file name: <input type="text" name="filename" size="30">
17 * <input type="submit" value="send" name="submit">
20 * This exact source code has not been verified to work.
26 #include <curl/curl.h>
27 #include <curl/types.h>
28 #include <curl/easy.h>
30 int main(int argc, char *argv[])
35 struct curl_httppost *formpost=NULL;
36 struct curl_httppost *lastptr=NULL;
37 struct curl_slist *headerlist=NULL;
38 static const char buf[] = "Expect:";
40 curl_global_init(CURL_GLOBAL_ALL);
42 /* Fill in the file upload field */
43 curl_formadd(&formpost,
45 CURLFORM_COPYNAME, "sendfile",
46 CURLFORM_FILE, "postit2.c",
49 /* Fill in the filename field */
50 curl_formadd(&formpost,
52 CURLFORM_COPYNAME, "filename",
53 CURLFORM_COPYCONTENTS, "postit2.c",
57 /* Fill in the submit field too, even if this is rarely needed */
58 curl_formadd(&formpost,
60 CURLFORM_COPYNAME, "submit",
61 CURLFORM_COPYCONTENTS, "send",
64 curl = curl_easy_init();
65 /* initalize custom header list (stating that Expect: 100-continue is not
67 headerlist = curl_slist_append(headerlist, buf);
69 /* what URL that receives this POST */
70 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/examplepost.cgi");
71 if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
72 /* only disable 100-continue header if explicitly requested */
73 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
74 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
75 res = curl_easy_perform(curl);
78 curl_easy_cleanup(curl);
80 /* then cleanup the formpost chain */
81 curl_formfree(formpost);
83 curl_slist_free_all (headerlist);