post-callback.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2015, 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  * An example source code that issues a HTTP POST and we provide the actual
00024  * data through a read callback.
00025  * </DESC>
00026  */
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <curl/curl.h>
00030 
00031 const char data[]="this is what we post to the silly web server";
00032 
00033 struct WriteThis {
00034   const char *readptr;
00035   long sizeleft;
00036 };
00037 
00038 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
00039 {
00040   struct WriteThis *pooh = (struct WriteThis *)userp;
00041 
00042   if(size*nmemb < 1)
00043     return 0;
00044 
00045   if(pooh->sizeleft) {
00046     *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
00047     pooh->readptr++;                 /* advance pointer */
00048     pooh->sizeleft--;                /* less data left */
00049     return 1;                        /* we return 1 byte at a time! */
00050   }
00051 
00052   return 0;                          /* no more data left to deliver */
00053 }
00054 
00055 int main(void)
00056 {
00057   CURL *curl;
00058   CURLcode res;
00059 
00060   struct WriteThis pooh;
00061 
00062   pooh.readptr = data;
00063   pooh.sizeleft = (long)strlen(data);
00064 
00065   /* In windows, this will init the winsock stuff */
00066   res = curl_global_init(CURL_GLOBAL_DEFAULT);
00067   /* Check for errors */
00068   if(res != CURLE_OK) {
00069     fprintf(stderr, "curl_global_init() failed: %s\n",
00070             curl_easy_strerror(res));
00071     return 1;
00072   }
00073 
00074   /* get a curl handle */
00075   curl = curl_easy_init();
00076   if(curl) {
00077     /* First set the URL that is about to receive our POST. */
00078     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/index.cgi");
00079 
00080     /* Now specify we want to POST data */
00081     curl_easy_setopt(curl, CURLOPT_POST, 1L);
00082 
00083     /* we want to use our own read function */
00084     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00085 
00086     /* pointer to pass to our read function */
00087     curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
00088 
00089     /* get verbose debug output please */
00090     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00091 
00092     /*
00093       If you use POST to a HTTP 1.1 server, you can send data without knowing
00094       the size before starting the POST if you use chunked encoding. You
00095       enable this by adding a header like "Transfer-Encoding: chunked" with
00096       CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
00097       specify the size in the request.
00098     */
00099 #ifdef USE_CHUNKED
00100     {
00101       struct curl_slist *chunk = NULL;
00102 
00103       chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
00104       res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
00105       /* use curl_slist_free_all() after the *perform() call to free this
00106          list again */
00107     }
00108 #else
00109     /* Set the expected POST size. If you want to POST large amounts of data,
00110        consider CURLOPT_POSTFIELDSIZE_LARGE */
00111     curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
00112 #endif
00113 
00114 #ifdef DISABLE_EXPECT
00115     /*
00116       Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
00117       header.  You can disable this header with CURLOPT_HTTPHEADER as usual.
00118       NOTE: if you want chunked transfer too, you need to combine these two
00119       since you can only set one list of headers with CURLOPT_HTTPHEADER. */
00120 
00121     /* A less good option would be to enforce HTTP 1.0, but that might also
00122        have other implications. */
00123     {
00124       struct curl_slist *chunk = NULL;
00125 
00126       chunk = curl_slist_append(chunk, "Expect:");
00127       res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
00128       /* use curl_slist_free_all() after the *perform() call to free this
00129          list again */
00130     }
00131 #endif
00132 
00133     /* Perform the request, res will get the return code */
00134     res = curl_easy_perform(curl);
00135     /* Check for errors */
00136     if(res != CURLE_OK)
00137       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00138               curl_easy_strerror(res));
00139 
00140     /* always cleanup */
00141     curl_easy_cleanup(curl);
00142   }
00143   curl_global_cleanup();
00144   return 0;
00145 }


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