Annotation of embedaddon/curl/docs/examples/post-callback.c, revision 1.1
1.1 ! misho 1: /***************************************************************************
! 2: * _ _ ____ _
! 3: * Project ___| | | | _ \| |
! 4: * / __| | | | |_) | |
! 5: * | (__| |_| | _ <| |___
! 6: * \___|\___/|_| \_\_____|
! 7: *
! 8: * Copyright (C) 1998 - 2017, 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 https://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: /* <DESC>
! 23: * Issue an HTTP POST and provide the data through the read callback.
! 24: * </DESC>
! 25: */
! 26: #include <stdio.h>
! 27: #include <string.h>
! 28: #include <curl/curl.h>
! 29:
! 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, "
! 37: "rhoncus ipsum.";
! 38:
! 39: struct WriteThis {
! 40: const char *readptr;
! 41: size_t sizeleft;
! 42: };
! 43:
! 44: static size_t read_callback(void *dest, size_t size, size_t nmemb, void *userp)
! 45: {
! 46: struct WriteThis *wt = (struct WriteThis *)userp;
! 47: size_t buffer_size = size*nmemb;
! 48:
! 49: if(wt->sizeleft) {
! 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);
! 55:
! 56: wt->readptr += copy_this_much;
! 57: wt->sizeleft -= copy_this_much;
! 58: return copy_this_much; /* we copied this many bytes */
! 59: }
! 60:
! 61: return 0; /* no more data left to deliver */
! 62: }
! 63:
! 64: int main(void)
! 65: {
! 66: CURL *curl;
! 67: CURLcode res;
! 68:
! 69: struct WriteThis wt;
! 70:
! 71: wt.readptr = data;
! 72: wt.sizeleft = strlen(data);
! 73:
! 74: /* In windows, this will init the winsock stuff */
! 75: res = curl_global_init(CURL_GLOBAL_DEFAULT);
! 76: /* Check for errors */
! 77: if(res != CURLE_OK) {
! 78: fprintf(stderr, "curl_global_init() failed: %s\n",
! 79: curl_easy_strerror(res));
! 80: return 1;
! 81: }
! 82:
! 83: /* get a curl handle */
! 84: curl = curl_easy_init();
! 85: if(curl) {
! 86: /* First set the URL that is about to receive our POST. */
! 87: curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/index.cgi");
! 88:
! 89: /* Now specify we want to POST data */
! 90: curl_easy_setopt(curl, CURLOPT_POST, 1L);
! 91:
! 92: /* we want to use our own read function */
! 93: curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
! 94:
! 95: /* pointer to pass to our read function */
! 96: curl_easy_setopt(curl, CURLOPT_READDATA, &wt);
! 97:
! 98: /* get verbose debug output please */
! 99: curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
! 100:
! 101: /*
! 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.
! 107: */
! 108: #ifdef USE_CHUNKED
! 109: {
! 110: struct curl_slist *chunk = NULL;
! 111:
! 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
! 115: list again */
! 116: }
! 117: #else
! 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);
! 121: #endif
! 122:
! 123: #ifdef DISABLE_EXPECT
! 124: /*
! 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. */
! 129:
! 130: /* A less good option would be to enforce HTTP 1.0, but that might also
! 131: have other implications. */
! 132: {
! 133: struct curl_slist *chunk = NULL;
! 134:
! 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
! 138: list again */
! 139: }
! 140: #endif
! 141:
! 142: /* Perform the request, res will get the return code */
! 143: res = curl_easy_perform(curl);
! 144: /* Check for errors */
! 145: if(res != CURLE_OK)
! 146: fprintf(stderr, "curl_easy_perform() failed: %s\n",
! 147: curl_easy_strerror(res));
! 148:
! 149: /* always cleanup */
! 150: curl_easy_cleanup(curl);
! 151: }
! 152: curl_global_cleanup();
! 153: return 0;
! 154: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>