httpput-postfields.c 3.4 KB

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