post-callback.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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. * An example source code that issues a HTTP POST and we provide the actual
  24. * data through a read callback.
  25. * </DESC>
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <curl/curl.h>
  30. static const char data[]="this is what we post to the silly web server";
  31. struct WriteThis {
  32. const char *readptr;
  33. long sizeleft;
  34. };
  35. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  36. {
  37. struct WriteThis *pooh = (struct WriteThis *)userp;
  38. if(size*nmemb < 1)
  39. return 0;
  40. if(pooh->sizeleft) {
  41. *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
  42. pooh->readptr++; /* advance pointer */
  43. pooh->sizeleft--; /* less data left */
  44. return 1; /* we return 1 byte at a time! */
  45. }
  46. return 0; /* no more data left to deliver */
  47. }
  48. int main(void)
  49. {
  50. CURL *curl;
  51. CURLcode res;
  52. struct WriteThis pooh;
  53. pooh.readptr = data;
  54. pooh.sizeleft = (long)strlen(data);
  55. /* In windows, this will init the winsock stuff */
  56. res = curl_global_init(CURL_GLOBAL_DEFAULT);
  57. /* Check for errors */
  58. if(res != CURLE_OK) {
  59. fprintf(stderr, "curl_global_init() failed: %s\n",
  60. curl_easy_strerror(res));
  61. return 1;
  62. }
  63. /* get a curl handle */
  64. curl = curl_easy_init();
  65. if(curl) {
  66. /* First set the URL that is about to receive our POST. */
  67. curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/index.cgi");
  68. /* Now specify we want to POST data */
  69. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  70. /* we want to use our own read function */
  71. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  72. /* pointer to pass to our read function */
  73. curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
  74. /* get verbose debug output please */
  75. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  76. /*
  77. If you use POST to a HTTP 1.1 server, you can send data without knowing
  78. the size before starting the POST if you use chunked encoding. You
  79. enable this by adding a header like "Transfer-Encoding: chunked" with
  80. CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
  81. specify the size in the request.
  82. */
  83. #ifdef USE_CHUNKED
  84. {
  85. struct curl_slist *chunk = NULL;
  86. chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
  87. res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  88. /* use curl_slist_free_all() after the *perform() call to free this
  89. list again */
  90. }
  91. #else
  92. /* Set the expected POST size. If you want to POST large amounts of data,
  93. consider CURLOPT_POSTFIELDSIZE_LARGE */
  94. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
  95. #endif
  96. #ifdef DISABLE_EXPECT
  97. /*
  98. Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
  99. header. You can disable this header with CURLOPT_HTTPHEADER as usual.
  100. NOTE: if you want chunked transfer too, you need to combine these two
  101. since you can only set one list of headers with CURLOPT_HTTPHEADER. */
  102. /* A less good option would be to enforce HTTP 1.0, but that might also
  103. have other implications. */
  104. {
  105. struct curl_slist *chunk = NULL;
  106. chunk = curl_slist_append(chunk, "Expect:");
  107. res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  108. /* use curl_slist_free_all() after the *perform() call to free this
  109. list again */
  110. }
  111. #endif
  112. /* Perform the request, res will get the return code */
  113. res = curl_easy_perform(curl);
  114. /* Check for errors */
  115. if(res != CURLE_OK)
  116. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  117. curl_easy_strerror(res));
  118. /* always cleanup */
  119. curl_easy_cleanup(curl);
  120. }
  121. curl_global_cleanup();
  122. return 0;
  123. }