1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * An example source code that issues a HTTP POST and we provide the actual
24 * data through a read callback.
29 #include <curl/curl.h>
31 const char data[]="this is what we post to the silly web server";
38 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
40 struct WriteThis *pooh = (struct WriteThis *)userp;
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! */
52 return 0; /* no more data left to deliver */
60 struct WriteThis pooh;
63 pooh.sizeleft = (long)strlen(data);
65 /* In windows, this will init the winsock stuff */
66 res = curl_global_init(CURL_GLOBAL_DEFAULT);
67 /* Check for errors */
69 fprintf(stderr, "curl_global_init() failed: %s\n",
70 curl_easy_strerror(res));
74 /* get a curl handle */
75 curl = curl_easy_init();
77 /* First set the URL that is about to receive our POST. */
78 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/index.cgi");
80 /* Now specify we want to POST data */
81 curl_easy_setopt(curl, CURLOPT_POST, 1L);
83 /* we want to use our own read function */
84 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
86 /* pointer to pass to our read function */
87 curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
89 /* get verbose debug output please */
90 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
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.
101 struct curl_slist *chunk = NULL;
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
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);
114 #ifdef DISABLE_EXPECT
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. */
121 /* A less good option would be to enforce HTTP 1.0, but that might also
122 have other implications. */
124 struct curl_slist *chunk = NULL;
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
133 /* Perform the request, res will get the return code */
134 res = curl_easy_perform(curl);
135 /* Check for errors */
137 fprintf(stderr, "curl_easy_perform() failed: %s\n",
138 curl_easy_strerror(res));
141 curl_easy_cleanup(curl);
143 curl_global_cleanup();