1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: postit2.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
10 * Example code that uploads a file name 'foo' to a remote script that accepts
11 * "HTML form based" (as described in RFC1738) uploads using HTTP POST.
13 * The imaginary form we'll fill in looks like:
15 * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
16 * Enter file: <input type="file" name="sendfile" size="40">
17 * Enter file name: <input type="text" name="filename" size="30">
18 * <input type="submit" value="send" name="submit">
21 * This exact source code has not been verified to work.
27 #include <curl/curl.h>
28 #include <curl/types.h>
29 #include <curl/easy.h>
31 int main(int argc
, char *argv
[])
36 struct curl_httppost
*formpost
=NULL
;
37 struct curl_httppost
*lastptr
=NULL
;
38 struct curl_slist
*headerlist
=NULL
;
39 static const char buf
[] = "Expect:";
41 curl_global_init(CURL_GLOBAL_ALL
);
43 /* Fill in the file upload field */
44 curl_formadd(&formpost
,
46 CURLFORM_COPYNAME
, "sendfile",
47 CURLFORM_FILE
, "postit2.c",
50 /* Fill in the filename field */
51 curl_formadd(&formpost
,
53 CURLFORM_COPYNAME
, "filename",
54 CURLFORM_COPYCONTENTS
, "postit2.c",
58 /* Fill in the submit field too, even if this is rarely needed */
59 curl_formadd(&formpost
,
61 CURLFORM_COPYNAME
, "submit",
62 CURLFORM_COPYCONTENTS
, "send",
65 curl
= curl_easy_init();
66 /* initalize custom header list (stating that Expect: 100-continue is not
68 headerlist
= curl_slist_append(headerlist
, buf
);
70 /* what URL that receives this POST */
71 curl_easy_setopt(curl
, CURLOPT_URL
, "http://curl.haxx.se/examplepost.cgi");
72 if ( (argc
== 2) && (!strcmp(argv
[1], "noexpectheader")) )
73 /* only disable 100-continue header if explicitly requested */
74 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, headerlist
);
75 curl_easy_setopt(curl
, CURLOPT_HTTPPOST
, formpost
);
76 res
= curl_easy_perform(curl
);
79 curl_easy_cleanup(curl
);
81 /* then cleanup the formpost chain */
82 curl_formfree(formpost
);
84 curl_slist_free_all (headerlist
);