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