Git init
[external/curl.git] / docs / examples / postit2.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  *
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.
11  *
12  * The imaginary form we'll fill in looks like:
13  *
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">
18  * </form>
19  *
20  * This exact source code has not been verified to work.
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <curl/curl.h>
27 #include <curl/types.h>
28 #include <curl/easy.h>
29
30 int main(int argc, char *argv[])
31 {
32   CURL *curl;
33   CURLcode res;
34
35   struct curl_httppost *formpost=NULL;
36   struct curl_httppost *lastptr=NULL;
37   struct curl_slist *headerlist=NULL;
38   static const char buf[] = "Expect:";
39
40   curl_global_init(CURL_GLOBAL_ALL);
41
42   /* Fill in the file upload field */
43   curl_formadd(&formpost,
44                &lastptr,
45                CURLFORM_COPYNAME, "sendfile",
46                CURLFORM_FILE, "postit2.c",
47                CURLFORM_END);
48
49   /* Fill in the filename field */
50   curl_formadd(&formpost,
51                &lastptr,
52                CURLFORM_COPYNAME, "filename",
53                CURLFORM_COPYCONTENTS, "postit2.c",
54                CURLFORM_END);
55
56
57   /* Fill in the submit field too, even if this is rarely needed */
58   curl_formadd(&formpost,
59                &lastptr,
60                CURLFORM_COPYNAME, "submit",
61                CURLFORM_COPYCONTENTS, "send",
62                CURLFORM_END);
63
64   curl = curl_easy_init();
65   /* initalize custom header list (stating that Expect: 100-continue is not
66      wanted */
67   headerlist = curl_slist_append(headerlist, buf);
68   if(curl) {
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);
76
77     /* always cleanup */
78     curl_easy_cleanup(curl);
79
80     /* then cleanup the formpost chain */
81     curl_formfree(formpost);
82     /* free slist */
83     curl_slist_free_all (headerlist);
84   }
85   return 0;
86 }