1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * HTTP Multipart formpost with file upload and two additional parts.
26 /* Example code that uploads a file name 'foo' to a remote script that accepts
27 * "HTML form based" (as described in RFC1738) uploads using HTTP POST.
29 * The imaginary form we'll fill in looks like:
31 * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
32 * Enter file: <input type="file" name="sendfile" size="40">
33 * Enter file name: <input type="text" name="filename" size="30">
34 * <input type="submit" value="send" name="submit">
37 * This exact source code has not been verified to work.
43 #include <curl/curl.h>
45 int main(int argc, char *argv[])
50 curl_mime *form = NULL;
51 curl_mimepart *field = NULL;
52 struct curl_slist *headerlist = NULL;
53 static const char buf[] = "Expect:";
55 curl_global_init(CURL_GLOBAL_ALL);
57 curl = curl_easy_init();
60 form = curl_mime_init(curl);
62 /* Fill in the file upload field */
63 field = curl_mime_addpart(form);
64 curl_mime_name(field, "sendfile");
65 curl_mime_filedata(field, "postit2.c");
67 /* Fill in the filename field */
68 field = curl_mime_addpart(form);
69 curl_mime_name(field, "filename");
70 curl_mime_data(field, "postit2.c", CURL_ZERO_TERMINATED);
72 /* Fill in the submit field too, even if this is rarely needed */
73 field = curl_mime_addpart(form);
74 curl_mime_name(field, "submit");
75 curl_mime_data(field, "send", CURL_ZERO_TERMINATED);
77 /* initialize custom header list (stating that Expect: 100-continue is not
79 headerlist = curl_slist_append(headerlist, buf);
80 /* what URL that receives this POST */
81 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/examplepost.cgi");
82 if((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
83 /* only disable 100-continue header if explicitly requested */
84 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
85 curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
87 /* Perform the request, res will get the return code */
88 res = curl_easy_perform(curl);
89 /* Check for errors */
91 fprintf(stderr, "curl_easy_perform() failed: %s\n",
92 curl_easy_strerror(res));
95 curl_easy_cleanup(curl);
97 /* then cleanup the form */
100 curl_slist_free_all(headerlist);