2 <title>curl_formadd man page
</title>
3 <meta name=
"generator" content=
"roffit 0.7">
4 <STYLE type=
"text/css">
30 background-color: #e0e0e0;
36 font-family: monospace;
46 <p class=
"level0"><a name=
"NAME"></a><h2 class=
"nroffsh">NAME
</h2>
47 <p class=
"level0">curl_formadd - add a section to a multipart/formdata HTTP POST
<a name=
"SYNOPSIS"></a><h2 class=
"nroffsh">SYNOPSIS
</h2>
48 <p class=
"level0"><span Class=
"bold">#include
<curl/curl.h
></span>
49 <p class=
"level0"><span Class=
"bold">CURLFORMcode curl_formadd(struct curl_httppost ** firstitem,
</span> <span Class=
"bold">struct curl_httppost ** lastitem, ...);
</span>
50 <p class=
"level0"><a name=
"DESCRIPTION"></a><h2 class=
"nroffsh">DESCRIPTION
</h2>
51 <p class=
"level0">curl_formadd() is used to append sections when building a multipart/formdata HTTP POST (sometimes referred to as rfc1867-style posts). Append one section at a time until you've added all the sections you want included and then you pass the
<span Class=
"emphasis">firstitem
</span> pointer as parameter to
<span Class=
"bold">CURLOPT_HTTPPOST
</span>.
<span Class=
"emphasis">lastitem
</span> is set after each call and on repeated invokes it should be left as set to allow repeated invokes to find the end of the list faster.
52 <p class=
"level0">After the
<span Class=
"emphasis">lastitem
</span> pointer follow the real arguments.
53 <p class=
"level0">The pointers
<span Class=
"emphasis">*firstitem
</span> and
<span Class=
"emphasis">*lastitem
</span> should both be pointing to NULL in the first call to this function. All list-data will be allocated by the function itself. You must call
<a class=
"emphasis" href=
"./curl_formfree.html">curl_formfree(
3)
</a> after the form post has been done to free the resources.
54 <p class=
"level0">Using POST with HTTP
1.1 implies the use of a
"Expect: 100-continue" header. You can disable this header with
<span Class=
"emphasis">CURLOPT_HTTPHEADER
</span> as usual.
55 <p class=
"level0">First, there are some basics you need to understand about multipart/formdata posts. Each part consists of at least a NAME and a CONTENTS part. If the part is made for file upload, there are also a stored CONTENT-TYPE and a FILENAME. Below, we'll discuss what options you use to set these properties in the parts you want to add to your post.
56 <p class=
"level0">The options listed first are for making normal parts. The options from
<a class=
"emphasis" href=
"#CURLFORMFILE">CURLFORM_FILE
</a> through
<a class=
"emphasis" href=
"#CURLFORMBUFFERLENGTH">CURLFORM_BUFFERLENGTH
</a> are for file upload parts.
<a name=
"OPTIONS"></a><h2 class=
"nroffsh">OPTIONS
</h2>
58 <p class=
"level0"><a name=
"CURLFORMCOPYNAME"></a><span class=
"nroffip">CURLFORM_COPYNAME
</span>
59 <p class=
"level1">followed by a string which provides the
<span Class=
"emphasis">name
</span> of this part. libcurl copies the string so your application doesn't need to keep it around after this function call. If the name isn't NUL-terminated, or if you'd like it to contain zero bytes, you must set its length with
<span Class=
"bold">CURLFORM_NAMELENGTH
</span>. The copied data will be freed by
<a class=
"emphasis" href=
"./curl_formfree.html">curl_formfree(
3)
</a>.
60 <p class=
"level0"><a name=
"CURLFORMPTRNAME"></a><span class=
"nroffip">CURLFORM_PTRNAME
</span>
61 <p class=
"level1">followed by a string which provides the
<span Class=
"emphasis">name
</span> of this part. libcurl will use the pointer and refer to the data in your application, so you must make sure it remains until curl no longer needs it. If the name isn't NUL-terminated, or if you'd like it to contain zero bytes, you must set its length with
<span Class=
"bold">CURLFORM_NAMELENGTH
</span>.
62 <p class=
"level0"><a name=
"CURLFORMCOPYCONTENTS"></a><span class=
"nroffip">CURLFORM_COPYCONTENTS
</span>
63 <p class=
"level1">followed by a pointer to the contents of this part, the actual data to send away. libcurl copies the provided data, so your application doesn't need to keep it around after this function call. If the data isn't null terminated, or if you'd like it to contain zero bytes, you must set the length of the name with
<a class=
"bold" href=
"#CURLFORMCONTENTSLENGTH">CURLFORM_CONTENTSLENGTH
</a>. The copied data will be freed by
<a class=
"emphasis" href=
"./curl_formfree.html">curl_formfree(
3)
</a>.
64 <p class=
"level0"><a name=
"CURLFORMPTRCONTENTS"></a><span class=
"nroffip">CURLFORM_PTRCONTENTS
</span>
65 <p class=
"level1">followed by a pointer to the contents of this part, the actual data to send away. libcurl will use the pointer and refer to the data in your application, so you must make sure it remains until curl no longer needs it. If the data isn't NUL-terminated, or if you'd like it to contain zero bytes, you must set its length with
<a class=
"bold" href=
"#CURLFORMCONTENTSLENGTH">CURLFORM_CONTENTSLENGTH
</a>.
66 <p class=
"level0"><a name=
"CURLFORMCONTENTSLENGTH"></a><span class=
"nroffip">CURLFORM_CONTENTSLENGTH
</span>
67 <p class=
"level1">followed by a long giving the length of the contents. Note that for
<a class=
"emphasis" href=
"#CURLFORMSTREAM">CURLFORM_STREAM
</a> contents, this option is mandatory.
68 <p class=
"level0"><a name=
"CURLFORMFILECONTENT"></a><span class=
"nroffip">CURLFORM_FILECONTENT
</span>
69 <p class=
"level1">followed by a filename, causes that file to be read and its contents used as data in this part. This part does
<span Class=
"emphasis">not
</span> automatically become a file upload part simply because its data was read from a file.
70 <p class=
"level0"><a name=
"CURLFORMFILE"></a><span class=
"nroffip">CURLFORM_FILE
</span>
71 <p class=
"level1">followed by a filename, makes this part a file upload part. It sets the
<span Class=
"emphasis">filename
</span> field to the basename of the provided filename, it reads the contents of the file and passes them as data and sets the content-type if the given file match one of the internally known file extensions. For
<a class=
"bold" href=
"#CURLFORMFILE">CURLFORM_FILE
</a> the user may send one or more files in one part by providing multiple
<a class=
"bold" href=
"#CURLFORMFILE">CURLFORM_FILE
</a> arguments each followed by the filename (and each
<a class=
"emphasis" href=
"#CURLFORMFILE">CURLFORM_FILE
</a> is allowed to have a
<a class=
"emphasis" href=
"#CURLFORMCONTENTTYPE">CURLFORM_CONTENTTYPE
</a>).
72 <p class=
"level0"><a name=
"CURLFORMCONTENTTYPE"></a><span class=
"nroffip">CURLFORM_CONTENTTYPE
</span>
73 <p class=
"level1">is used in combination with
<a class=
"emphasis" href=
"#CURLFORMFILE">CURLFORM_FILE
</a>. Followed by a pointer to a string which provides the content-type for this part, possibly instead of an internally chosen one.
74 <p class=
"level0"><a name=
"CURLFORMFILENAME"></a><span class=
"nroffip">CURLFORM_FILENAME
</span>
75 <p class=
"level1">is used in combination with
<a class=
"emphasis" href=
"#CURLFORMFILE">CURLFORM_FILE
</a>. Followed by a pointer to a string, it tells libcurl to use the given string as the
<span Class=
"emphasis">filename
</span> in the file upload part instead of the actual file name.
76 <p class=
"level0"><a name=
"CURLFORMBUFFER"></a><span class=
"nroffip">CURLFORM_BUFFER
</span>
77 <p class=
"level1">is used for custom file upload parts without use of
<a class=
"emphasis" href=
"#CURLFORMFILE">CURLFORM_FILE
</a>. It tells libcurl that the file contents are already present in a buffer. The parameter is a string which provides the
<span Class=
"emphasis">filename
</span> field in the content header.
78 <p class=
"level0"><a name=
"CURLFORMBUFFERPTR"></a><span class=
"nroffip">CURLFORM_BUFFERPTR
</span>
79 <p class=
"level1">is used in combination with
<a class=
"emphasis" href=
"#CURLFORMBUFFER">CURLFORM_BUFFER
</a>. The parameter is a pointer to the buffer to be uploaded. This buffer must not be freed until after
<a class=
"emphasis" href=
"./curl_easy_cleanup.html">curl_easy_cleanup(
3)
</a> is called. You must also use
<a class=
"emphasis" href=
"#CURLFORMBUFFERLENGTH">CURLFORM_BUFFERLENGTH
</a> to set the number of bytes in the buffer.
80 <p class=
"level0"><a name=
"CURLFORMBUFFERLENGTH"></a><span class=
"nroffip">CURLFORM_BUFFERLENGTH
</span>
81 <p class=
"level1">is used in combination with
<a class=
"emphasis" href=
"#CURLFORMBUFFER">CURLFORM_BUFFER
</a>. The parameter is a long which gives the length of the buffer.
82 <p class=
"level0"><a name=
"CURLFORMSTREAM"></a><span class=
"nroffip">CURLFORM_STREAM
</span>
83 <p class=
"level1">Tells libcurl to use the
<span Class=
"emphasis">CURLOPT_READFUNCTION
</span> callback to get data. The parameter you pass to
<a class=
"emphasis" href=
"#CURLFORMSTREAM">CURLFORM_STREAM
</a> is the pointer passed on to the read callback's fourth argument. If you want the part to look like a file upload one, set the
<a class=
"emphasis" href=
"#CURLFORMFILENAME">CURLFORM_FILENAME
</a> parameter as well. (Option added in libcurl
7.18.2)
84 <p class=
"level0"><a name=
"CURLFORMARRAY"></a><span class=
"nroffip">CURLFORM_ARRAY
</span>
85 <p class=
"level1">Another possibility to send options to curl_formadd() is the
<a class=
"bold" href=
"#CURLFORMARRAY">CURLFORM_ARRAY
</a> option, that passes a struct curl_forms array pointer as its value. Each curl_forms structure element has a CURLformoption and a char pointer. The final element in the array must be a CURLFORM_END. All available options can be used in an array, except the CURLFORM_ARRAY option itself! The last argument in such an array must always be
<span Class=
"bold">CURLFORM_END
</span>.
86 <p class=
"level0"><a name=
"CURLFORMCONTENTHEADER"></a><span class=
"nroffip">CURLFORM_CONTENTHEADER
</span>
87 <p class=
"level1">specifies extra headers for the form POST section. This takes a curl_slist prepared in the usual way using
<span Class=
"bold">curl_slist_append
</span> and appends the list of headers to those libcurl automatically generates. The list must exist while the POST occurs, if you free it before the post completes you may experience problems.
88 <p class=
"level1">When you've passed the HttpPost pointer to
<a class=
"emphasis" href=
"./curl_easy_setopt.html">curl_easy_setopt(
3)
</a> (using the
<span Class=
"emphasis">CURLOPT_HTTPPOST
</span> option), you must not free the list until after you've called
<a class=
"emphasis" href=
"./curl_easy_cleanup.html">curl_easy_cleanup(
3)
</a> for the curl handle.
89 <p class=
"level1">See example below.
<a name=
"RETURN"></a><h2 class=
"nroffsh">RETURN VALUE
</h2>
90 <p class=
"level0">0 means everything was ok, non-zero means an error occurred corresponding to a CURL_FORMADD_* constant defined in
<span Class=
"emphasis"><curl/curl.h
></span> <a name=
"EXAMPLE"></a><h2 class=
"nroffsh">EXAMPLE
</h2>
91 <p class=
"level0"><pre>
92 <p class=
"level0"><p class=
"level0"> struct curl_httppost* post = NULL;
93 struct curl_httppost* last = NULL;
94 char namebuffer[] =
"name buffer";
95 long namelength = strlen(namebuffer);
96 char buffer[] =
"test buffer";
97 char htmlbuffer[] =
"<HTML>test buffer</HTML>";
98 long htmlbufferlength = strlen(htmlbuffer);
99 struct curl_forms forms[
3];
100 char file1[] =
"my-face.jpg";
101 char file2[] =
"your-face.jpg";
102 /* add null character into htmlbuffer, to demonstrate that
103 transfers of buffers containing null characters actually work
105 htmlbuffer[
8] = '\
0';
106 <p class=
"level0"> /* Add simple name/content section */
107 curl_formadd(&post, &last, CURLFORM_COPYNAME,
"name",
108 CURLFORM_COPYCONTENTS,
"content", CURLFORM_END);
109 <p class=
"level0"> /* Add simple name/content/contenttype section */
110 curl_formadd(&post, &last, CURLFORM_COPYNAME,
"htmlcode",
111 CURLFORM_COPYCONTENTS,
"<HTML></HTML>",
112 CURLFORM_CONTENTTYPE,
"text/html", CURLFORM_END);
113 <p class=
"level0"> /* Add name/ptrcontent section */
114 curl_formadd(&post, &last, CURLFORM_COPYNAME,
"name_for_ptrcontent",
115 CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
116 <p class=
"level0"> /* Add ptrname/ptrcontent section */
117 curl_formadd(&post, &last, CURLFORM_PTRNAME, namebuffer,
118 CURLFORM_PTRCONTENTS, buffer, CURLFORM_NAMELENGTH,
119 namelength, CURLFORM_END);
120 <p class=
"level0"> /* Add name/ptrcontent/contenttype section */
121 curl_formadd(&post, &last, CURLFORM_COPYNAME,
"html_code_with_hole",
122 CURLFORM_PTRCONTENTS, htmlbuffer,
123 CURLFORM_CONTENTSLENGTH, htmlbufferlength,
124 CURLFORM_CONTENTTYPE,
"text/html", CURLFORM_END);
125 <p class=
"level0"> /* Add simple file section */
126 curl_formadd(&post, &last, CURLFORM_COPYNAME,
"picture",
127 CURLFORM_FILE,
"my-face.jpg", CURLFORM_END);
128 <p class=
"level0"> /* Add file/contenttype section */
129 curl_formadd(&post, &last, CURLFORM_COPYNAME,
"picture",
130 CURLFORM_FILE,
"my-face.jpg",
131 CURLFORM_CONTENTTYPE,
"image/jpeg", CURLFORM_END);
132 <p class=
"level0"> /* Add two file section */
133 curl_formadd(&post, &last, CURLFORM_COPYNAME,
"pictures",
134 CURLFORM_FILE,
"my-face.jpg",
135 CURLFORM_FILE,
"your-face.jpg", CURLFORM_END);
136 <p class=
"level0"> /* Add two file section using CURLFORM_ARRAY */
137 forms[
0].option = CURLFORM_FILE;
138 forms[
0].value = file1;
139 forms[
1].option = CURLFORM_FILE;
140 forms[
1].value = file2;
141 forms[
2].option = CURLFORM_END;
142 <p class=
"level0"> /* Add a buffer to upload */
143 curl_formadd(&post, &last,
144 CURLFORM_COPYNAME,
"name",
145 CURLFORM_BUFFER,
"data",
146 CURLFORM_BUFFERPTR, record,
147 CURLFORM_BUFFERLENGTH, record_length,
148 CURLFORM_END);
149 <p class=
"level0"> /* no option needed for the end marker */
150 curl_formadd(&post, &last, CURLFORM_COPYNAME,
"pictures",
151 CURLFORM_ARRAY, forms, CURLFORM_END);
152 /* Add the content of a file as a normal post text value */
153 curl_formadd(&post, &last, CURLFORM_COPYNAME,
"filecontent",
154 CURLFORM_FILECONTENT,
".bashrc", CURLFORM_END);
155 /* Set the form info */
156 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
157 <p class=
"level0"></pre>
158 <a name=
"SEE"></a><h2 class=
"nroffsh">SEE ALSO
</h2>
159 <p class=
"level0"><a class=
"manpage" href=
"./curl_easy_setopt.html">curl_easy_setopt (
3)
</a> <span Class=
"manpage"> </span> <a class=
"manpage" href=
"./curl_formfree.html">curl_formfree (
3)
</a> <p class=
"roffit">
160 This HTML page was made with
<a href=
"http://daniel.haxx.se/projects/roffit/">roffit
</a>.