quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

postit2-formadd.c (3896B)


      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 Multipart formpost with file upload and two additional parts.
     26  * </DESC>
     27  */
     28 
     29 /*
     30  * Example code that uploads a filename 'foo' to a remote script that accepts
     31  * "HTML form based" (as described in RFC 1738) uploads using HTTP POST.
     32  *
     33  * Warning: this example uses the deprecated form api. See "postit2.c"
     34  *          for a similar example using the mime api.
     35  *
     36  * The imaginary form we fill in looks like:
     37  *
     38  * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
     39  * Enter file: <input type="file" name="sendfile" size="40">
     40  * Enter filename: <input type="text" name="filename" size="30">
     41  * <input type="submit" value="send" name="submit">
     42  * </form>
     43  */
     44 
     45 #include <stdio.h>
     46 #include <string.h>
     47 
     48 #include <curl/curl.h>
     49 
     50 int main(int argc, char *argv[])
     51 {
     52   CURL *curl;
     53   CURLcode res;
     54 
     55   struct curl_httppost *formpost = NULL;
     56   struct curl_httppost *lastptr = NULL;
     57   struct curl_slist *headerlist = NULL;
     58   static const char buf[] = "Expect:";
     59 
     60   curl_global_init(CURL_GLOBAL_ALL);
     61 
     62   CURL_IGNORE_DEPRECATION(
     63     /* Fill in the file upload field */
     64     curl_formadd(&formpost,
     65                  &lastptr,
     66                  CURLFORM_COPYNAME, "sendfile",
     67                  CURLFORM_FILE, "postit2-formadd.c",
     68                  CURLFORM_END);
     69 
     70     /* Fill in the filename field */
     71     curl_formadd(&formpost,
     72                  &lastptr,
     73                  CURLFORM_COPYNAME, "filename",
     74                  CURLFORM_COPYCONTENTS, "postit2-formadd.c",
     75                  CURLFORM_END);
     76 
     77 
     78     /* Fill in the submit field too, even if this is rarely needed */
     79     curl_formadd(&formpost,
     80                  &lastptr,
     81                  CURLFORM_COPYNAME, "submit",
     82                  CURLFORM_COPYCONTENTS, "send",
     83                  CURLFORM_END);
     84   )
     85 
     86   curl = curl_easy_init();
     87   /* initialize custom header list (stating that Expect: 100-continue is not
     88      wanted */
     89   headerlist = curl_slist_append(headerlist, buf);
     90   if(curl) {
     91     /* what URL that receives this POST */
     92     curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/examplepost.cgi");
     93     if((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
     94       /* only disable 100-continue header if explicitly requested */
     95       curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
     96     CURL_IGNORE_DEPRECATION(
     97       curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
     98     )
     99 
    100     /* Perform the request, res gets the return code */
    101     res = curl_easy_perform(curl);
    102     /* Check for errors */
    103     if(res != CURLE_OK)
    104       fprintf(stderr, "curl_easy_perform() failed: %s\n",
    105               curl_easy_strerror(res));
    106 
    107     /* always cleanup */
    108     curl_easy_cleanup(curl);
    109 
    110     CURL_IGNORE_DEPRECATION(
    111       /* then cleanup the formpost chain */
    112       curl_formfree(formpost);
    113     )
    114 
    115     /* free slist */
    116     curl_slist_free_all(headerlist);
    117   }
    118   return 0;
    119 }