httpput-postfields.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  22. /* <DESC>
  23. * HTTP PUT using CURLOPT_POSTFIELDS
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <fcntl.h>
  28. #include <sys/stat.h>
  29. #include <curl/curl.h>
  30. static const char olivertwist[]=
  31. "Among other public buildings in a certain town, which for many reasons "
  32. "it will be prudent to refrain from mentioning, and to which I will assign "
  33. "no fictitious name, there is one anciently common to most towns, great or "
  34. "small: to wit, a workhouse; and in this workhouse was born; on a day and "
  35. "date which I need not trouble myself to repeat, inasmuch as it can be of "
  36. "no possible consequence to the reader, in this stage of the business at "
  37. "all events; the item of mortality whose name is prefixed to the head of "
  38. "this chapter.";
  39. /*
  40. * This example shows a HTTP PUT operation that sends a fixed buffer with
  41. * CURLOPT_POSTFIELDS to the URL given as an argument.
  42. */
  43. int main(int argc, char **argv)
  44. {
  45. CURL *curl;
  46. CURLcode res;
  47. char *url;
  48. if(argc < 2)
  49. return 1;
  50. url = argv[1];
  51. /* In windows, this will init the winsock stuff */
  52. curl_global_init(CURL_GLOBAL_ALL);
  53. /* get a curl handle */
  54. curl = curl_easy_init();
  55. if(curl) {
  56. struct curl_slist *headers = NULL;
  57. /* default type with postfields is application/x-www-form-urlencoded,
  58. change it if you want */
  59. headers = curl_slist_append(headers, "Content-Type: literature/classic");
  60. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  61. /* pass on content in request body. When CURLOPT_POSTFIELDSIZE isn't used,
  62. curl does strlen to get the size. */
  63. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, olivertwist);
  64. /* override the POST implied by CURLOPT_POSTFIELDS
  65. *
  66. * Warning: CURLOPT_CUSTOMREQUEST is problematic, especially if you want
  67. * to follow redirects. Be aware.
  68. */
  69. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
  70. /* specify target URL, and note that this URL should include a file
  71. name, not only a directory */
  72. curl_easy_setopt(curl, CURLOPT_URL, url);
  73. /* Now run off and do what you've been told! */
  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. /* always cleanup */
  80. curl_easy_cleanup(curl);
  81. /* free headers */
  82. curl_slist_free_all(headers);
  83. }
  84. curl_global_cleanup();
  85. return 0;
  86. }