httpput-postfields.c 3.4 KB

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