post-callback.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Issue an HTTP POST and provide the data through the read callback.
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <curl/curl.h>
  31. /* silly test data to POST */
  32. static const char data[]="Lorem ipsum dolor sit amet, consectetur adipiscing "
  33. "elit. Sed vel urna neque. Ut quis leo metus. Quisque eleifend, ex at "
  34. "laoreet rhoncus, odio ipsum semper metus, at tempus ante urna in mauris. "
  35. "Suspendisse ornare tempor venenatis. Ut dui neque, pellentesque a varius "
  36. "eget, mattis vitae ligula. Fusce ut pharetra est. Ut ullamcorper mi ac "
  37. "sollicitudin semper. Praesent sit amet tellus varius, posuere nulla non, "
  38. "rhoncus ipsum.";
  39. struct WriteThis {
  40. const char *readptr;
  41. size_t sizeleft;
  42. };
  43. static size_t read_callback(char *dest, size_t size, size_t nmemb, void *userp)
  44. {
  45. struct WriteThis *wt = (struct WriteThis *)userp;
  46. size_t buffer_size = size*nmemb;
  47. if(wt->sizeleft) {
  48. /* copy as much as possible from the source to the destination */
  49. size_t copy_this_much = wt->sizeleft;
  50. if(copy_this_much > buffer_size)
  51. copy_this_much = buffer_size;
  52. memcpy(dest, wt->readptr, copy_this_much);
  53. wt->readptr += copy_this_much;
  54. wt->sizeleft -= copy_this_much;
  55. return copy_this_much; /* we copied this many bytes */
  56. }
  57. return 0; /* no more data left to deliver */
  58. }
  59. int main(void)
  60. {
  61. CURL *curl;
  62. CURLcode res;
  63. struct WriteThis wt;
  64. wt.readptr = data;
  65. wt.sizeleft = strlen(data);
  66. /* In windows, this will init the winsock stuff */
  67. res = curl_global_init(CURL_GLOBAL_DEFAULT);
  68. /* Check for errors */
  69. if(res != CURLE_OK) {
  70. fprintf(stderr, "curl_global_init() failed: %s\n",
  71. curl_easy_strerror(res));
  72. return 1;
  73. }
  74. /* get a curl handle */
  75. curl = curl_easy_init();
  76. if(curl) {
  77. /* First set the URL that is about to receive our POST. */
  78. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/index.cgi");
  79. /* Now specify we want to POST data */
  80. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  81. /* we want to use our own read function */
  82. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  83. /* pointer to pass to our read function */
  84. curl_easy_setopt(curl, CURLOPT_READDATA, &wt);
  85. /* get verbose debug output please */
  86. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  87. /*
  88. If you use POST to an HTTP 1.1 server, you can send data without knowing
  89. the size before starting the POST if you use chunked encoding. You
  90. enable this by adding a header like "Transfer-Encoding: chunked" with
  91. CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
  92. specify the size in the request.
  93. */
  94. #ifdef USE_CHUNKED
  95. {
  96. struct curl_slist *chunk = NULL;
  97. chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
  98. res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  99. /* use curl_slist_free_all() after the *perform() call to free this
  100. list again */
  101. }
  102. #else
  103. /* Set the expected POST size. If you want to POST large amounts of data,
  104. consider CURLOPT_POSTFIELDSIZE_LARGE */
  105. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)wt.sizeleft);
  106. #endif
  107. #ifdef DISABLE_EXPECT
  108. /*
  109. Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
  110. header. You can disable this header with CURLOPT_HTTPHEADER as usual.
  111. NOTE: if you want chunked transfer too, you need to combine these two
  112. since you can only set one list of headers with CURLOPT_HTTPHEADER. */
  113. /* A less good option would be to enforce HTTP 1.0, but that might also
  114. have other implications. */
  115. {
  116. struct curl_slist *chunk = NULL;
  117. chunk = curl_slist_append(chunk, "Expect:");
  118. res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  119. /* use curl_slist_free_all() after the *perform() call to free this
  120. list again */
  121. }
  122. #endif
  123. /* Perform the request, res will get the return code */
  124. res = curl_easy_perform(curl);
  125. /* Check for errors */
  126. if(res != CURLE_OK)
  127. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  128. curl_easy_strerror(res));
  129. /* always cleanup */
  130. curl_easy_cleanup(curl);
  131. }
  132. curl_global_cleanup();
  133. return 0;
  134. }