Annotation of embedaddon/curl/docs/examples/postit2.c, revision 1.1

1.1     ! misho       1: /***************************************************************************
        !             2:  *                                  _   _ ____  _
        !             3:  *  Project                     ___| | | |  _ \| |
        !             4:  *                             / __| | | | |_) | |
        !             5:  *                            | (__| |_| |  _ <| |___
        !             6:  *                             \___|\___/|_| \_\_____|
        !             7:  *
        !             8:  * Copyright (C) 1998 - 2019, 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.haxx.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 Multipart formpost with file upload and two additional parts.
        !            24:  * </DESC>
        !            25:  */
        !            26: /* Example code that uploads a file name 'foo' to a remote script that accepts
        !            27:  * "HTML form based" (as described in RFC1738) uploads using HTTP POST.
        !            28:  *
        !            29:  * The imaginary form we'll fill in looks like:
        !            30:  *
        !            31:  * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
        !            32:  * Enter file: <input type="file" name="sendfile" size="40">
        !            33:  * Enter file name: <input type="text" name="filename" size="30">
        !            34:  * <input type="submit" value="send" name="submit">
        !            35:  * </form>
        !            36:  *
        !            37:  */
        !            38: 
        !            39: #include <stdio.h>
        !            40: #include <string.h>
        !            41: 
        !            42: #include <curl/curl.h>
        !            43: 
        !            44: int main(int argc, char *argv[])
        !            45: {
        !            46:   CURL *curl;
        !            47:   CURLcode res;
        !            48: 
        !            49:   curl_mime *form = NULL;
        !            50:   curl_mimepart *field = NULL;
        !            51:   struct curl_slist *headerlist = NULL;
        !            52:   static const char buf[] = "Expect:";
        !            53: 
        !            54:   curl_global_init(CURL_GLOBAL_ALL);
        !            55: 
        !            56:   curl = curl_easy_init();
        !            57:   if(curl) {
        !            58:     /* Create the form */
        !            59:     form = curl_mime_init(curl);
        !            60: 
        !            61:     /* Fill in the file upload field */
        !            62:     field = curl_mime_addpart(form);
        !            63:     curl_mime_name(field, "sendfile");
        !            64:     curl_mime_filedata(field, "postit2.c");
        !            65: 
        !            66:     /* Fill in the filename field */
        !            67:     field = curl_mime_addpart(form);
        !            68:     curl_mime_name(field, "filename");
        !            69:     curl_mime_data(field, "postit2.c", CURL_ZERO_TERMINATED);
        !            70: 
        !            71:     /* Fill in the submit field too, even if this is rarely needed */
        !            72:     field = curl_mime_addpart(form);
        !            73:     curl_mime_name(field, "submit");
        !            74:     curl_mime_data(field, "send", CURL_ZERO_TERMINATED);
        !            75: 
        !            76:     /* initialize custom header list (stating that Expect: 100-continue is not
        !            77:        wanted */
        !            78:     headerlist = curl_slist_append(headerlist, buf);
        !            79:     /* what URL that receives this POST */
        !            80:     curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/examplepost.cgi");
        !            81:     if((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
        !            82:       /* only disable 100-continue header if explicitly requested */
        !            83:       curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
        !            84:     curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
        !            85: 
        !            86:     /* Perform the request, res will get the return code */
        !            87:     res = curl_easy_perform(curl);
        !            88:     /* Check for errors */
        !            89:     if(res != CURLE_OK)
        !            90:       fprintf(stderr, "curl_easy_perform() failed: %s\n",
        !            91:               curl_easy_strerror(res));
        !            92: 
        !            93:     /* always cleanup */
        !            94:     curl_easy_cleanup(curl);
        !            95: 
        !            96:     /* then cleanup the form */
        !            97:     curl_mime_free(form);
        !            98:     /* free slist */
        !            99:     curl_slist_free_all(headerlist);
        !           100:   }
        !           101:   return 0;
        !           102: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>