postit2.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * HTTP Multipart formpost with file upload and two additional parts.
00024  * </DESC>
00025  */
00026 /* Example code that uploads a file name 'foo' to a remote script that accepts
00027  * "HTML form based" (as described in RFC1738) uploads using HTTP POST.
00028  *
00029  * The imaginary form we'll fill in looks like:
00030  *
00031  * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
00032  * Enter file: <input type="file" name="sendfile" size="40">
00033  * Enter file name: <input type="text" name="filename" size="30">
00034  * <input type="submit" value="send" name="submit">
00035  * </form>
00036  *
00037  * This exact source code has not been verified to work.
00038  */
00039 
00040 #include <stdio.h>
00041 #include <string.h>
00042 
00043 #include <curl/curl.h>
00044 
00045 int main(int argc, char *argv[])
00046 {
00047   CURL *curl;
00048   CURLcode res;
00049 
00050   struct curl_httppost *formpost=NULL;
00051   struct curl_httppost *lastptr=NULL;
00052   struct curl_slist *headerlist=NULL;
00053   static const char buf[] = "Expect:";
00054 
00055   curl_global_init(CURL_GLOBAL_ALL);
00056 
00057   /* Fill in the file upload field */
00058   curl_formadd(&formpost,
00059                &lastptr,
00060                CURLFORM_COPYNAME, "sendfile",
00061                CURLFORM_FILE, "postit2.c",
00062                CURLFORM_END);
00063 
00064   /* Fill in the filename field */
00065   curl_formadd(&formpost,
00066                &lastptr,
00067                CURLFORM_COPYNAME, "filename",
00068                CURLFORM_COPYCONTENTS, "postit2.c",
00069                CURLFORM_END);
00070 
00071 
00072   /* Fill in the submit field too, even if this is rarely needed */
00073   curl_formadd(&formpost,
00074                &lastptr,
00075                CURLFORM_COPYNAME, "submit",
00076                CURLFORM_COPYCONTENTS, "send",
00077                CURLFORM_END);
00078 
00079   curl = curl_easy_init();
00080   /* initialize custom header list (stating that Expect: 100-continue is not
00081      wanted */
00082   headerlist = curl_slist_append(headerlist, buf);
00083   if(curl) {
00084     /* what URL that receives this POST */
00085     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/examplepost.cgi");
00086     if((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
00087       /* only disable 100-continue header if explicitly requested */
00088       curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
00089     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
00090 
00091     /* Perform the request, res will get the return code */
00092     res = curl_easy_perform(curl);
00093     /* Check for errors */
00094     if(res != CURLE_OK)
00095       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00096               curl_easy_strerror(res));
00097 
00098     /* always cleanup */
00099     curl_easy_cleanup(curl);
00100 
00101     /* then cleanup the formpost chain */
00102     curl_formfree(formpost);
00103     /* free slist */
00104     curl_slist_free_all(headerlist);
00105   }
00106   return 0;
00107 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06