postinmemory.c 3.4 KB

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