1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 https://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 * Issue an HTTP POST and provide the data through the read callback.
28 #include <curl/curl.h>
30 /* silly test data to POST */
31 static const char data[]="Lorem ipsum dolor sit amet, consectetur adipiscing "
32 "elit. Sed vel urna neque. Ut quis leo metus. Quisque eleifend, ex at "
33 "laoreet rhoncus, odio ipsum semper metus, at tempus ante urna in mauris. "
34 "Suspendisse ornare tempor venenatis. Ut dui neque, pellentesque a varius "
35 "eget, mattis vitae ligula. Fusce ut pharetra est. Ut ullamcorper mi ac "
36 "sollicitudin semper. Praesent sit amet tellus varius, posuere nulla non, "
44 static size_t read_callback(void *dest, size_t size, size_t nmemb, void *userp)
46 struct WriteThis *wt = (struct WriteThis *)userp;
47 size_t buffer_size = size*nmemb;
50 /* copy as much as possible from the source to the destination */
51 size_t copy_this_much = wt->sizeleft;
52 if(copy_this_much > buffer_size)
53 copy_this_much = buffer_size;
54 memcpy(dest, wt->readptr, copy_this_much);
56 wt->readptr += copy_this_much;
57 wt->sizeleft -= copy_this_much;
58 return copy_this_much; /* we copied this many bytes */
61 return 0; /* no more data left to deliver */
72 wt.sizeleft = strlen(data);
74 /* In windows, this will init the winsock stuff */
75 res = curl_global_init(CURL_GLOBAL_DEFAULT);
76 /* Check for errors */
78 fprintf(stderr, "curl_global_init() failed: %s\n",
79 curl_easy_strerror(res));
83 /* get a curl handle */
84 curl = curl_easy_init();
86 /* First set the URL that is about to receive our POST. */
87 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/index.cgi");
89 /* Now specify we want to POST data */
90 curl_easy_setopt(curl, CURLOPT_POST, 1L);
92 /* we want to use our own read function */
93 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
95 /* pointer to pass to our read function */
96 curl_easy_setopt(curl, CURLOPT_READDATA, &wt);
98 /* get verbose debug output please */
99 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
102 If you use POST to a HTTP 1.1 server, you can send data without knowing
103 the size before starting the POST if you use chunked encoding. You
104 enable this by adding a header like "Transfer-Encoding: chunked" with
105 CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
106 specify the size in the request.
110 struct curl_slist *chunk = NULL;
112 chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
113 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
114 /* use curl_slist_free_all() after the *perform() call to free this
118 /* Set the expected POST size. If you want to POST large amounts of data,
119 consider CURLOPT_POSTFIELDSIZE_LARGE */
120 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)wt.sizeleft);
123 #ifdef DISABLE_EXPECT
125 Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
126 header. You can disable this header with CURLOPT_HTTPHEADER as usual.
127 NOTE: if you want chunked transfer too, you need to combine these two
128 since you can only set one list of headers with CURLOPT_HTTPHEADER. */
130 /* A less good option would be to enforce HTTP 1.0, but that might also
131 have other implications. */
133 struct curl_slist *chunk = NULL;
135 chunk = curl_slist_append(chunk, "Expect:");
136 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
137 /* use curl_slist_free_all() after the *perform() call to free this
142 /* Perform the request, res will get the return code */
143 res = curl_easy_perform(curl);
144 /* Check for errors */
146 fprintf(stderr, "curl_easy_perform() failed: %s\n",
147 curl_easy_strerror(res));
150 curl_easy_cleanup(curl);
152 curl_global_cleanup();