1 .\" You can view this file with:
3 .\" $Id: curl_formadd.3,v 1.15 2006-09-03 22:12:57 bagder Exp $
5 .TH curl_formadd 3 "24 June 2002" "libcurl 7.9.8" "libcurl Manual"
7 curl_formadd - add a section to a multipart/formdata HTTP POST
9 .B #include <curl/curl.h>
11 .BI "CURLFORMcode curl_formadd(struct curl_httppost ** " firstitem,
12 .BI "struct curl_httppost ** " lastitem, " ...);"
15 curl_formadd() is used to append sections when building a multipart/formdata
16 HTTP POST (sometimes referred to as rfc1867-style posts). Append one section at
17 a time until you've added all the sections you want included and then you pass
18 the \fIfirstitem\fP pointer as parameter to \fBCURLOPT_HTTPPOST\fP.
19 \fIlastitem\fP is set after each call and on repeated invokes it should be
20 left as set to allow repeated invokes to find the end of the list faster.
22 After the \fIlastitem\fP pointer follow the real arguments.
24 The pointers \fI*firstitem\fP and \fI*lastitem\fP should both be pointing to
25 NULL in the first call to this function. All list-data will be allocated by
26 the function itself. You must call \fIcurl_formfree(3)\fP after the form post
27 has been done to free the resources.
29 Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue" header.
30 You can disable this header with \fICURLOPT_HTTPHEADER\fP as usual.
32 First, there are some basics you need to understand about multipart/formdata
33 posts. Each part consists of at least a NAME and a CONTENTS part. If the part
34 is made for file upload, there are also a stored CONTENT-TYPE and a FILENAME.
35 Below, we'll discuss what options you use to set these properties in the
36 parts you want to add to your post.
38 The options listed first are for making normal parts. The options from
39 \fICURLFORM_FILE\fP through \fICURLFORM_BUFFERLENGTH\fP are for file upload
45 followed by a string which provides the \fIname\fP of this part. libcurl
46 copies the string so your application doesn't need to keep it around after
47 this function call. If the name isn't null terminated, or if you'd
48 like it to contain zero bytes, you must set its length with
49 \fBCURLFORM_NAMELENGTH\fP. The copied data will be freed by
50 \fIcurl_formfree(3)\fP.
53 followed by a string which provides the \fIname\fP of this part. libcurl
54 will use the pointer and refer to the data in your application, so you
55 must make sure it remains until curl no longer needs it. If the name
56 isn't null terminated, or if you'd like it to contain zero
57 bytes, you must set its length with \fBCURLFORM_NAMELENGTH\fP.
59 .IP CURLFORM_COPYCONTENTS
60 followed by a pointer to the contents of this part, the actual data
61 to send away. libcurl copies the provided data, so your application doesn't
62 need to keep it around after this function call. If the data isn't null
63 terminated, or if you'd like it to contain zero bytes, you must
64 set the length of the name with \fBCURLFORM_CONTENTSLENGTH\fP. The copied
65 data will be freed by \fIcurl_formfree(3)\fP.
67 .IP CURLFORM_PTRCONTENTS
68 followed by a pointer to the contents of this part, the actual data
69 to send away. libcurl will use the pointer and refer to the data in your
70 application, so you must make sure it remains until curl no longer needs it.
71 If the data isn't null terminated, or if you'd like it to contain zero bytes,
72 you must set its length with \fBCURLFORM_CONTENTSLENGTH\fP.
74 .IP CURLFORM_CONTENTSLENGTH
75 followed by a long giving the length of the contents.
77 .IP CURLFORM_FILECONTENT
78 followed by a filename, causes that file to be read and its contents used
79 as data in this part. This part does \fInot\fP automatically become a file
80 upload part simply because its data was read from a file.
83 followed by a filename, makes this part a file upload part. It sets the
84 \fIfilename\fP field to the basename of the provided filename, it reads the
85 contents of the file and passes them as data and sets the content-type if the
86 given file match one of the internally known file extensions. For
87 \fBCURLFORM_FILE\fP the user may send one or more files in one part by
88 providing multiple \fBCURLFORM_FILE\fP arguments each followed by the
89 filename (and each CURLFORM_FILE is allowed to have a CURLFORM_CONTENTTYPE).
91 .IP CURLFORM_CONTENTTYPE
92 is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a
93 string which provides the content-type for this part, possibly instead of an
94 internally chosen one.
97 is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a
98 string, it tells libcurl to use the given string as the \fIfilename\fP in the
99 file upload part instead of the actual file name.
102 is used for custom file upload parts without use of \fICURLFORM_FILE\fP. It
103 tells libcurl that the file contents are already present in a buffer. The
104 parameter is a string which provides the \fIfilename\fP field in the content
107 .IP CURLFORM_BUFFERPTR
108 is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a pointer
109 to the buffer to be uploaded. This buffer must not be freed until after
110 \fIcurl_easy_cleanup(3)\fP is called. You must also use
111 \fICURLFORM_BUFFERLENGTH\fP to set the number of bytes in the buffer.
113 .IP CURLFORM_BUFFERLENGTH
114 is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a
115 long which gives the length of the buffer.
118 Another possibility to send options to curl_formadd() is the
119 \fBCURLFORM_ARRAY\fP option, that passes a struct curl_forms array pointer as
120 its value. Each curl_forms structure element has a CURLformoption and a char
121 pointer. The final element in the array must be a CURLFORM_END. All available
122 options can be used in an array, except the CURLFORM_ARRAY option itself! The
123 last argument in such an array must always be \fBCURLFORM_END\fP.
125 .IP CURLFORM_CONTENTHEADER
126 specifies extra headers for the form POST section. This takes a curl_slist
127 prepared in the usual way using \fBcurl_slist_append\fP and appends the list
128 of headers to those libcurl automatically generates. The list must exist while
129 the POST occurs, if you free it before the post completes you may experience
132 When you've passed the HttpPost pointer to \fIcurl_easy_setopt(3)\fP (using
133 the \fICURLOPT_HTTPPOST\fP option), you must not free the list until after
134 you've called \fIcurl_easy_cleanup(3)\fP for the curl handle.
138 0 means everything was ok, non-zero means an error occurred as
144 struct curl_httppost* post = NULL;
145 struct curl_httppost* last = NULL;
146 char namebuffer[] = "name buffer";
147 long namelength = strlen(namebuffer);
148 char buffer[] = "test buffer";
149 char htmlbuffer[] = "<HTML>test buffer</HTML>";
150 long htmlbufferlength = strlen(htmlbuffer);
151 struct curl_forms forms[3];
152 char file1[] = "my-face.jpg";
153 char file2[] = "your-face.jpg";
154 /* add null character into htmlbuffer, to demonstrate that
155 transfers of buffers containing null characters actually work
157 htmlbuffer[8] = '\\0';
159 /* Add simple name/content section */
160 curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
161 CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
163 /* Add simple name/content/contenttype section */
164 curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
165 CURLFORM_COPYCONTENTS, "<HTML></HTML>",
166 CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
168 /* Add name/ptrcontent section */
169 curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
170 CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
172 /* Add ptrname/ptrcontent section */
173 curl_formadd(&post, &last, CURLFORM_PTRNAME, namebuffer,
174 CURLFORM_PTRCONTENTS, buffer, CURLFORM_NAMELENGTH,
175 namelength, CURLFORM_END);
177 /* Add name/ptrcontent/contenttype section */
178 curl_formadd(&post, &last, CURLFORM_COPYNAME, "html_code_with_hole",
179 CURLFORM_PTRCONTENTS, htmlbuffer,
180 CURLFORM_CONTENTSLENGTH, htmlbufferlength,
181 CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
183 /* Add simple file section */
184 curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
185 CURLFORM_FILE, "my-face.jpg", CURLFORM_END);
187 /* Add file/contenttype section */
188 curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
189 CURLFORM_FILE, "my-face.jpg",
190 CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END);
192 /* Add two file section */
193 curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures",
194 CURLFORM_FILE, "my-face.jpg",
195 CURLFORM_FILE, "your-face.jpg", CURLFORM_END);
197 /* Add two file section using CURLFORM_ARRAY */
198 forms[0].option = CURLFORM_FILE;
199 forms[0].value = file1;
200 forms[1].option = CURLFORM_FILE;
201 forms[1].value = file2;
202 forms[2].option = CURLFORM_END;
204 /* Add a buffer to upload */
205 curl_formadd(&post, &last,
206 CURLFORM_COPYNAME, "name",
207 CURLFORM_BUFFER, "data",
208 CURLFORM_BUFFERPTR, record,
209 CURLFORM_BUFFERLENGTH, record_length,
212 /* no option needed for the end marker */
213 curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures",
214 CURLFORM_ARRAY, forms, CURLFORM_END);
215 /* Add the content of a file as a normal post text value */
216 curl_formadd(&post, &last, CURLFORM_COPYNAME, "filecontent",
217 CURLFORM_FILECONTENT, ".bashrc", CURLFORM_END);
218 /* Set the form info */
219 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
222 .BR curl_easy_setopt "(3), "
223 .BR curl_formfree "(3)"