post-callback.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * An example source code that issues a HTTP POST and we provide the actual
  11. * data through a read callback.
  12. *
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <curl/curl.h>
  17. const char data[]="this is what we post to the silly web server";
  18. struct WriteThis {
  19. const char *readptr;
  20. int sizeleft;
  21. };
  22. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  23. {
  24. struct WriteThis *pooh = (struct WriteThis *)userp;
  25. if(size*nmemb < 1)
  26. return 0;
  27. if(pooh->sizeleft) {
  28. *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
  29. pooh->readptr++; /* advance pointer */
  30. pooh->sizeleft--; /* less data left */
  31. return 1; /* we return 1 byte at a time! */
  32. }
  33. return 0; /* no more data left to deliver */
  34. }
  35. int main(void)
  36. {
  37. CURL *curl;
  38. CURLcode res;
  39. struct WriteThis pooh;
  40. pooh.readptr = data;
  41. pooh.sizeleft = strlen(data);
  42. curl = curl_easy_init();
  43. if(curl) {
  44. /* First set the URL that is about to receive our POST. */
  45. curl_easy_setopt(curl, CURLOPT_URL,
  46. "http://receivingsite.com.pooh/index.cgi");
  47. /* Now specify we want to POST data */
  48. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  49. /* we want to use our own read function */
  50. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  51. /* pointer to pass to our read function */
  52. curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
  53. /* get verbose debug output please */
  54. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  55. /*
  56. If you use POST to a HTTP 1.1 server, you can send data without knowing
  57. the size before starting the POST if you use chunked encoding. You
  58. enable this by adding a header like "Transfer-Encoding: chunked" with
  59. CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
  60. specify the size in the request.
  61. */
  62. #ifdef USE_CHUNKED
  63. {
  64. struct curl_slist *chunk = NULL;
  65. chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
  66. res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  67. /* use curl_slist_free_all() after the *perform() call to free this
  68. list again */
  69. }
  70. #else
  71. /* Set the expected POST size. If you want to POST large amounts of data,
  72. consider CURLOPT_POSTFIELDSIZE_LARGE */
  73. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (curl_off_t)pooh.sizeleft);
  74. #endif
  75. #ifdef DISABLE_EXPECT
  76. /*
  77. Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
  78. header. You can disable this header with CURLOPT_HTTPHEADER as usual.
  79. NOTE: if you want chunked transfer too, you need to combine these two
  80. since you can only set one list of headers with CURLOPT_HTTPHEADER. */
  81. /* A less good option would be to enforce HTTP 1.0, but that might also
  82. have other implications. */
  83. {
  84. struct curl_slist *chunk = NULL;
  85. chunk = curl_slist_append(chunk, "Expect:");
  86. res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  87. /* use curl_slist_free_all() after the *perform() call to free this
  88. list again */
  89. }
  90. #endif
  91. /* Perform the request, res will get the return code */
  92. res = curl_easy_perform(curl);
  93. /* always cleanup */
  94. curl_easy_cleanup(curl);
  95. }
  96. return 0;
  97. }