Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / postit2.c
blob61b2499520bae139aca9fcc2b12e83eb7ee48d31
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
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">
19 * </form>
21 * This exact source code has not been verified to work.
24 #include <stdio.h>
25 #include <string.h>
27 #include <curl/curl.h>
28 #include <curl/types.h>
29 #include <curl/easy.h>
31 int main(int argc, char *argv[])
33 CURL *curl;
34 CURLcode res;
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,
45 &lastptr,
46 CURLFORM_COPYNAME, "sendfile",
47 CURLFORM_FILE, "postit2.c",
48 CURLFORM_END);
50 /* Fill in the filename field */
51 curl_formadd(&formpost,
52 &lastptr,
53 CURLFORM_COPYNAME, "filename",
54 CURLFORM_COPYCONTENTS, "postit2.c",
55 CURLFORM_END);
58 /* Fill in the submit field too, even if this is rarely needed */
59 curl_formadd(&formpost,
60 &lastptr,
61 CURLFORM_COPYNAME, "submit",
62 CURLFORM_COPYCONTENTS, "send",
63 CURLFORM_END);
65 curl = curl_easy_init();
66 /* initalize custom header list (stating that Expect: 100-continue is not
67 wanted */
68 headerlist = curl_slist_append(headerlist, buf);
69 if(curl) {
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);
78 /* always cleanup */
79 curl_easy_cleanup(curl);
81 /* then cleanup the formpost chain */
82 curl_formfree(formpost);
83 /* free slist */
84 curl_slist_free_all (headerlist);
86 return 0;