post-callback.c 4.7 KB

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