postinmemory.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * Make a HTTP POST with data from memory and receive response in memory.
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <curl/curl.h>
  30. struct MemoryStruct {
  31. char *memory;
  32. size_t size;
  33. };
  34. static size_t
  35. WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
  36. {
  37. size_t realsize = size * nmemb;
  38. struct MemoryStruct *mem = (struct MemoryStruct *)userp;
  39. mem->memory = realloc(mem->memory, mem->size + realsize + 1);
  40. if(mem->memory == NULL) {
  41. /* out of memory! */
  42. printf("not enough memory (realloc returned NULL)\n");
  43. return 0;
  44. }
  45. memcpy(&(mem->memory[mem->size]), contents, realsize);
  46. mem->size += realsize;
  47. mem->memory[mem->size] = 0;
  48. return realsize;
  49. }
  50. int main(void)
  51. {
  52. CURL *curl;
  53. CURLcode res;
  54. struct MemoryStruct chunk;
  55. static const char *postthis = "Field=1&Field=2&Field=3";
  56. chunk.memory = malloc(1); /* will be grown as needed by realloc above */
  57. chunk.size = 0; /* no data at this point */
  58. curl_global_init(CURL_GLOBAL_ALL);
  59. curl = curl_easy_init();
  60. if(curl) {
  61. curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.org/");
  62. /* send all data to this function */
  63. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  64. /* we pass our 'chunk' struct to the callback function */
  65. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
  66. /* some servers don't like requests that are made without a user-agent
  67. field, so we provide one */
  68. curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  69. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
  70. /* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
  71. itself */
  72. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
  73. /* Perform the request, res will get the return code */
  74. res = curl_easy_perform(curl);
  75. /* Check for errors */
  76. if(res != CURLE_OK) {
  77. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  78. curl_easy_strerror(res));
  79. }
  80. else {
  81. /*
  82. * Now, our chunk.memory points to a memory block that is chunk.size
  83. * bytes big and contains the remote file.
  84. *
  85. * Do something nice with it!
  86. */
  87. printf("%s\n",chunk.memory);
  88. }
  89. /* always cleanup */
  90. curl_easy_cleanup(curl);
  91. free(chunk.memory);
  92. /* we're done with libcurl, so clean it up */
  93. curl_global_cleanup();
  94. }
  95. return 0;
  96. }