Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2015, 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 https://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 /* <DESC>
     23  * An example source code that issues a HTTP POST and we provide the actual
     24  * data through a read callback.
     25  * </DESC>
     26  */
     27 #include <stdio.h>
     28 #include <string.h>
     29 #include <curl/curl.h>
     30 
     31 const char data[]="this is what we post to the silly web server";
     32 
     33 struct WriteThis {
     34   const char *readptr;
     35   long sizeleft;
     36 };
     37 
     38 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
     39 {
     40   struct WriteThis *pooh = (struct WriteThis *)userp;
     41 
     42   if(size*nmemb < 1)
     43     return 0;
     44 
     45   if(pooh->sizeleft) {
     46     *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
     47     pooh->readptr++;                 /* advance pointer */
     48     pooh->sizeleft--;                /* less data left */
     49     return 1;                        /* we return 1 byte at a time! */
     50   }
     51 
     52   return 0;                          /* no more data left to deliver */
     53 }
     54 
     55 int main(void)
     56 {
     57   CURL *curl;
     58   CURLcode res;
     59 
     60   struct WriteThis pooh;
     61 
     62   pooh.readptr = data;
     63   pooh.sizeleft = (long)strlen(data);
     64 
     65   /* In windows, this will init the winsock stuff */
     66   res = curl_global_init(CURL_GLOBAL_DEFAULT);
     67   /* Check for errors */
     68   if(res != CURLE_OK) {
     69     fprintf(stderr, "curl_global_init() failed: %s\n",
     70             curl_easy_strerror(res));
     71     return 1;
     72   }
     73 
     74   /* get a curl handle */
     75   curl = curl_easy_init();
     76   if(curl) {
     77     /* First set the URL that is about to receive our POST. */
     78     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/index.cgi");
     79 
     80     /* Now specify we want to POST data */
     81     curl_easy_setopt(curl, CURLOPT_POST, 1L);
     82 
     83     /* we want to use our own read function */
     84     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
     85 
     86     /* pointer to pass to our read function */
     87     curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
     88 
     89     /* get verbose debug output please */
     90     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
     91 
     92     /*
     93       If you use POST to a HTTP 1.1 server, you can send data without knowing
     94       the size before starting the POST if you use chunked encoding. You
     95       enable this by adding a header like "Transfer-Encoding: chunked" with
     96       CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
     97       specify the size in the request.
     98     */
     99 #ifdef USE_CHUNKED
    100     {
    101       struct curl_slist *chunk = NULL;
    102 
    103       chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
    104       res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    105       /* use curl_slist_free_all() after the *perform() call to free this
    106          list again */
    107     }
    108 #else
    109     /* Set the expected POST size. If you want to POST large amounts of data,
    110        consider CURLOPT_POSTFIELDSIZE_LARGE */
    111     curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
    112 #endif
    113 
    114 #ifdef DISABLE_EXPECT
    115     /*
    116       Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
    117       header.  You can disable this header with CURLOPT_HTTPHEADER as usual.
    118       NOTE: if you want chunked transfer too, you need to combine these two
    119       since you can only set one list of headers with CURLOPT_HTTPHEADER. */
    120 
    121     /* A less good option would be to enforce HTTP 1.0, but that might also
    122        have other implications. */
    123     {
    124       struct curl_slist *chunk = NULL;
    125 
    126       chunk = curl_slist_append(chunk, "Expect:");
    127       res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    128       /* use curl_slist_free_all() after the *perform() call to free this
    129          list again */
    130     }
    131 #endif
    132 
    133     /* Perform the request, res will get the return code */
    134     res = curl_easy_perform(curl);
    135     /* Check for errors */
    136     if(res != CURLE_OK)
    137       fprintf(stderr, "curl_easy_perform() failed: %s\n",
    138               curl_easy_strerror(res));
    139 
    140     /* always cleanup */
    141     curl_easy_cleanup(curl);
    142   }
    143   curl_global_cleanup();
    144   return 0;
    145 }
    146