Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel (at) haxx.se>, et al.
      9  *
     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 http://curl.haxx.se/docs/copyright.html.
     13  *
     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.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  ***************************************************************************/
     22 /* Example code that uploads a file name 'foo' to a remote script that accepts
     23  * "HTML form based" (as described in RFC1738) uploads using HTTP POST.
     24  *
     25  * The imaginary form we'll fill in looks like:
     26  *
     27  * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
     28  * Enter file: <input type="file" name="sendfile" size="40">
     29  * Enter file name: <input type="text" name="filename" size="30">
     30  * <input type="submit" value="send" name="submit">
     31  * </form>
     32  *
     33  * This exact source code has not been verified to work.
     34  */
     35 
     36 #include <stdio.h>
     37 #include <string.h>
     38 
     39 #include <curl/curl.h>
     40 
     41 int main(int argc, char *argv[])
     42 {
     43   CURL *curl;
     44   CURLcode res;
     45 
     46   struct curl_httppost *formpost=NULL;
     47   struct curl_httppost *lastptr=NULL;
     48   struct curl_slist *headerlist=NULL;
     49   static const char buf[] = "Expect:";
     50 
     51   curl_global_init(CURL_GLOBAL_ALL);
     52 
     53   /* Fill in the file upload field */
     54   curl_formadd(&formpost,
     55                &lastptr,
     56                CURLFORM_COPYNAME, "sendfile",
     57                CURLFORM_FILE, "postit2.c",
     58                CURLFORM_END);
     59 
     60   /* Fill in the filename field */
     61   curl_formadd(&formpost,
     62                &lastptr,
     63                CURLFORM_COPYNAME, "filename",
     64                CURLFORM_COPYCONTENTS, "postit2.c",
     65                CURLFORM_END);
     66 
     67 
     68   /* Fill in the submit field too, even if this is rarely needed */
     69   curl_formadd(&formpost,
     70                &lastptr,
     71                CURLFORM_COPYNAME, "submit",
     72                CURLFORM_COPYCONTENTS, "send",
     73                CURLFORM_END);
     74 
     75   curl = curl_easy_init();
     76   /* initialize custom header list (stating that Expect: 100-continue is not
     77      wanted */
     78   headerlist = curl_slist_append(headerlist, buf);
     79   if(curl) {
     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_HTTPPOST, formpost);
     86 
     87     /* Perform the request, res will get the return code */
     88     res = curl_easy_perform(curl);
     89     /* Check for errors */
     90     if(res != CURLE_OK)
     91       fprintf(stderr, "curl_easy_perform() failed: %s\n",
     92               curl_easy_strerror(res));
     93 
     94     /* always cleanup */
     95     curl_easy_cleanup(curl);
     96 
     97     /* then cleanup the formpost chain */
     98     curl_formfree(formpost);
     99     /* free slist */
    100     curl_slist_free_all (headerlist);
    101   }
    102   return 0;
    103 }
    104