113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci/* <DESC>
2513498266Sopenharmony_ci * HTTP PUT using CURLOPT_POSTFIELDS
2613498266Sopenharmony_ci * </DESC>
2713498266Sopenharmony_ci */
2813498266Sopenharmony_ci#include <stdio.h>
2913498266Sopenharmony_ci#include <fcntl.h>
3013498266Sopenharmony_ci#include <sys/stat.h>
3113498266Sopenharmony_ci#include <curl/curl.h>
3213498266Sopenharmony_ci
3313498266Sopenharmony_cistatic const char olivertwist[]=
3413498266Sopenharmony_ci  "Among other public buildings in a certain town, which for many reasons "
3513498266Sopenharmony_ci  "it will be prudent to refrain from mentioning, and to which I will assign "
3613498266Sopenharmony_ci  "no fictitious name, there is one anciently common to most towns, great or "
3713498266Sopenharmony_ci  "small: to wit, a workhouse; and in this workhouse was born; on a day and "
3813498266Sopenharmony_ci  "date which I need not trouble myself to repeat, inasmuch as it can be of "
3913498266Sopenharmony_ci  "no possible consequence to the reader, in this stage of the business at "
4013498266Sopenharmony_ci  "all events; the item of mortality whose name is prefixed";
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci/* ... to the head of this chapter. String cut off to stick within the C90
4313498266Sopenharmony_ci   509 byte limit. */
4413498266Sopenharmony_ci
4513498266Sopenharmony_ci/*
4613498266Sopenharmony_ci * This example shows an HTTP PUT operation that sends a fixed buffer with
4713498266Sopenharmony_ci * CURLOPT_POSTFIELDS to the URL given as an argument.
4813498266Sopenharmony_ci */
4913498266Sopenharmony_ci
5013498266Sopenharmony_ciint main(int argc, char **argv)
5113498266Sopenharmony_ci{
5213498266Sopenharmony_ci  CURL *curl;
5313498266Sopenharmony_ci  CURLcode res;
5413498266Sopenharmony_ci  char *url;
5513498266Sopenharmony_ci
5613498266Sopenharmony_ci  if(argc < 2)
5713498266Sopenharmony_ci    return 1;
5813498266Sopenharmony_ci
5913498266Sopenharmony_ci  url = argv[1];
6013498266Sopenharmony_ci
6113498266Sopenharmony_ci  /* In windows, this will init the winsock stuff */
6213498266Sopenharmony_ci  curl_global_init(CURL_GLOBAL_ALL);
6313498266Sopenharmony_ci
6413498266Sopenharmony_ci  /* get a curl handle */
6513498266Sopenharmony_ci  curl = curl_easy_init();
6613498266Sopenharmony_ci  if(curl) {
6713498266Sopenharmony_ci    struct curl_slist *headers = NULL;
6813498266Sopenharmony_ci
6913498266Sopenharmony_ci    /* default type with postfields is application/x-www-form-urlencoded,
7013498266Sopenharmony_ci       change it if you want */
7113498266Sopenharmony_ci    headers = curl_slist_append(headers, "Content-Type: literature/classic");
7213498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci    /* pass on content in request body. When CURLOPT_POSTFIELDSIZE is not used,
7513498266Sopenharmony_ci       curl does strlen to get the size. */
7613498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, olivertwist);
7713498266Sopenharmony_ci
7813498266Sopenharmony_ci    /* override the POST implied by CURLOPT_POSTFIELDS
7913498266Sopenharmony_ci     *
8013498266Sopenharmony_ci     * Warning: CURLOPT_CUSTOMREQUEST is problematic, especially if you want
8113498266Sopenharmony_ci     * to follow redirects. Be aware.
8213498266Sopenharmony_ci     */
8313498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci    /* specify target URL, and note that this URL should include a file
8613498266Sopenharmony_ci       name, not only a directory */
8713498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_URL, url);
8813498266Sopenharmony_ci
8913498266Sopenharmony_ci    /* Now run off and do what you have been told! */
9013498266Sopenharmony_ci    res = curl_easy_perform(curl);
9113498266Sopenharmony_ci    /* Check for errors */
9213498266Sopenharmony_ci    if(res != CURLE_OK)
9313498266Sopenharmony_ci      fprintf(stderr, "curl_easy_perform() failed: %s\n",
9413498266Sopenharmony_ci              curl_easy_strerror(res));
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci    /* always cleanup */
9713498266Sopenharmony_ci    curl_easy_cleanup(curl);
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci    /* free headers */
10013498266Sopenharmony_ci    curl_slist_free_all(headers);
10113498266Sopenharmony_ci  }
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci  curl_global_cleanup();
10413498266Sopenharmony_ci  return 0;
10513498266Sopenharmony_ci}
106