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
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 to the head of "
39 "this chapter.";
40
41 /*
42 * This example shows a HTTP PUT operation that sends a fixed buffer with
43 * CURLOPT_POSTFIELDS to the URL given as an argument.
44 */
45
main(int argc,char ** argv)46 int main(int argc, char **argv)
47 {
48 CURL *curl;
49 CURLcode res;
50 char *url;
51
52 if(argc < 2)
53 return 1;
54
55 url = argv[1];
56
57 /* In windows, this will init the winsock stuff */
58 curl_global_init(CURL_GLOBAL_ALL);
59
60 /* get a curl handle */
61 curl = curl_easy_init();
62 if(curl) {
63 struct curl_slist *headers = NULL;
64
65 /* default type with postfields is application/x-www-form-urlencoded,
66 change it if you want */
67 headers = curl_slist_append(headers, "Content-Type: literature/classic");
68 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
69
70 /* pass on content in request body. When CURLOPT_POSTFIELDSIZE isn't used,
71 curl does strlen to get the size. */
72 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, olivertwist);
73
74 /* override the POST implied by CURLOPT_POSTFIELDS
75 *
76 * Warning: CURLOPT_CUSTOMREQUEST is problematic, especially if you want
77 * to follow redirects. Be aware.
78 */
79 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
80
81 /* specify target URL, and note that this URL should include a file
82 name, not only a directory */
83 curl_easy_setopt(curl, CURLOPT_URL, url);
84
85 /* Now run off and do what you've been told! */
86 res = curl_easy_perform(curl);
87 /* Check for errors */
88 if(res != CURLE_OK)
89 fprintf(stderr, "curl_easy_perform() failed: %s\n",
90 curl_easy_strerror(res));
91
92 /* always cleanup */
93 curl_easy_cleanup(curl);
94
95 /* free headers */
96 curl_slist_free_all(headers);
97 }
98
99 curl_global_cleanup();
100 return 0;
101 }
102