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