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