2
0

post-callback.c 5.2 KB

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