postit2-formadd.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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  * This exact source code has not been verified to work.
38  */
39 
40 #include <stdio.h>
41 #include <string.h>
42 
43 #include <curl/curl.h>
44 
45 int main(int argc, char *argv[])
46 {
47  CURL *curl;
48  CURLcode res;
49 
50  struct curl_httppost *formpost = NULL;
51  struct curl_httppost *lastptr = NULL;
52  struct curl_slist *headerlist = NULL;
53  static const char buf[] = "Expect:";
54 
56 
57  /* Fill in the file upload field */
58  curl_formadd(&formpost,
59  &lastptr,
60  CURLFORM_COPYNAME, "sendfile",
61  CURLFORM_FILE, "postit2.c",
62  CURLFORM_END);
63 
64  /* Fill in the filename field */
65  curl_formadd(&formpost,
66  &lastptr,
67  CURLFORM_COPYNAME, "filename",
68  CURLFORM_COPYCONTENTS, "postit2.c",
69  CURLFORM_END);
70 
71 
72  /* Fill in the submit field too, even if this is rarely needed */
73  curl_formadd(&formpost,
74  &lastptr,
75  CURLFORM_COPYNAME, "submit",
76  CURLFORM_COPYCONTENTS, "send",
77  CURLFORM_END);
78 
79  curl = curl_easy_init();
80  /* initialize custom header list (stating that Expect: 100-continue is not
81  wanted */
82  headerlist = curl_slist_append(headerlist, buf);
83  if(curl) {
84  /* what URL that receives this POST */
85  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/examplepost.cgi");
86  if((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
87  /* only disable 100-continue header if explicitly requested */
88  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
89  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
90 
91  /* Perform the request, res will get the return code */
92  res = curl_easy_perform(curl);
93  /* Check for errors */
94  if(res != CURLE_OK)
95  fprintf(stderr, "curl_easy_perform() failed: %s\n",
96  curl_easy_strerror(res));
97 
98  /* always cleanup */
99  curl_easy_cleanup(curl);
100 
101  /* then cleanup the formpost chain */
102  curl_formfree(formpost);
103  /* free slist */
104  curl_slist_free_all(headerlist);
105  }
106  return 0;
107 }
CURLcode
Definition: curl.h:454
static int res
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
CURL_EXTERN void curl_formfree(struct curl_httppost *form)
Definition: formdata.c:800
CURL_EXTERN CURLFORMcode curl_formadd(struct curl_httppost **httppost, struct curl_httppost **last_post,...)
Definition: formdata.c:743
CURL_EXTERN struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: slist.c:89
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
Definition: curl.h:455
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
int main(int argc, char *argv[])
char buf[3]
Definition: unit1398.c:32
void CURL
Definition: curl.h:102
#define fprintf
Definition: curl_printf.h:41
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
static CURL * curl
Definition: sessioninfo.c:35
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16