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