<pclass="level0">curl_formadd() is used to append sections when building a multipart/formdata HTTP POST (sometimes referred to as <ahref="http://www.ietf.org/rfc/rfc2388.txt">RFC 2388</a>-style posts). Append one section at a time until you've added all the sections you want included and then you pass the <spanClass="emphasis">firstitem</span> pointer as parameter to <spanClass="bold">CURLOPT_HTTPPOST</span>. <spanClass="emphasis">lastitem</span> is set after each <aclass="emphasis"href="./curl_formadd.html">curl_formadd(3)</a> call and on repeated invokes it should be left as set to allow repeated invokes to find the end of the list faster.
<pclass="level0">The pointers <spanClass="emphasis">firstitem</span> and <spanClass="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 <aclass="emphasis"href="./curl_formfree.html">curl_formfree(3)</a> on the <spanclass="emphasis">firstitemP after the form post has been done to free the resources.
<pclass="level0">Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue" header. You can disable this header with <spanClass="emphasis">CURLOPT_HTTPHEADER</span> as usual.
<pclass="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.
<pclass="level0">The options listed first are for making normal parts. The options from <aclass="emphasis"href="#CURLFORMFILE">CURLFORM_FILE</a> through <aclass="emphasis"href="#CURLFORMBUFFERLENGTH">CURLFORM_BUFFERLENGTH</a> are for file upload parts. <aname="OPTIONS"></a><h2class="nroffsh">OPTIONS</h2>
<pclass="level1">followed by a string which provides the <spanClass="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 <spanClass="bold">CURLFORM_NAMELENGTH</span>. The copied data will be freed by <aclass="emphasis"href="./curl_formfree.html">curl_formfree(3)</a>.
<pclass="level1">followed by a string which provides the <spanClass="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 <spanClass="bold">CURLFORM_NAMELENGTH</span>.
<pclass="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 <aclass="bold"href="#CURLFORMCONTENTSLENGTH">CURLFORM_CONTENTSLENGTH</a>. The copied data will be freed by <aclass="emphasis"href="./curl_formfree.html">curl_formfree(3)</a>.
<pclass="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 <aclass="bold"href="#CURLFORMCONTENTSLENGTH">CURLFORM_CONTENTSLENGTH</a>.
<pclass="level1">followed by a long giving the length of the contents. Note that for <aclass="emphasis"href="#CURLFORMSTREAM">CURLFORM_STREAM</a> contents, this option is mandatory.
<pclass="level1">followed by a filename, causes that file to be read and its contents used as data in this part. This part does <spanClass="emphasis">not</span> automatically become a file upload part simply because its data was read from a file.
<pclass="level1">followed by a filename, makes this part a file upload part. It sets the <spanClass="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 <aclass="bold"href="#CURLFORMFILE">CURLFORM_FILE</a> the user may send one or more files in one part by providing multiple <aclass="bold"href="#CURLFORMFILE">CURLFORM_FILE</a> arguments each followed by the filename (and each <aclass="emphasis"href="#CURLFORMFILE">CURLFORM_FILE</a> is allowed to have a <aclass="emphasis"href="#CURLFORMCONTENTTYPE">CURLFORM_CONTENTTYPE</a>).
<pclass="level1">is used in combination with <aclass="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.
<pclass="level1">is used in combination with <aclass="emphasis"href="#CURLFORMFILE">CURLFORM_FILE</a>. Followed by a pointer to a string, it tells libcurl to use the given string as the <spanClass="emphasis">filename</span> in the file upload part instead of the actual file name.
<pclass="level1">is used for custom file upload parts without use of <aclass="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 <spanClass="emphasis">filename</span> field in the content header.
<pclass="level1">is used in combination with <aclass="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 <aclass="emphasis"href="./curl_easy_cleanup.html">curl_easy_cleanup(3)</a> is called. You must also use <aclass="emphasis"href="#CURLFORMBUFFERLENGTH">CURLFORM_BUFFERLENGTH</a> to set the number of bytes in the buffer.
<pclass="level1">is used in combination with <aclass="emphasis"href="#CURLFORMBUFFER">CURLFORM_BUFFER</a>. The parameter is a long which gives the length of the buffer.
<pclass="level1">Tells libcurl to use the <spanClass="emphasis">CURLOPT_READFUNCTION</span> callback to get data. The parameter you pass to <aclass="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 <aclass="emphasis"href="#CURLFORMFILENAME">CURLFORM_FILENAME</a> parameter as well. Note that when using <aclass="emphasis"href="#CURLFORMSTREAM">CURLFORM_STREAM</a>, <aclass="emphasis"href="#CURLFORMCONTENTSLENGTH">CURLFORM_CONTENTSLENGTH</a> must also be set with the total expected length of the part. (Option added in libcurl 7.18.2)
<pclass="level1">Another possibility to send options to curl_formadd() is the <aclass="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 <spanClass="bold">CURLFORM_END</span>.
<pclass="level1">specifies extra headers for the form POST section. This takes a curl_slist prepared in the usual way using <spanClass="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.
<pclass="level1">When you've passed the HttpPost pointer to <aclass="emphasis"href="./curl_easy_setopt.html">curl_easy_setopt(3)</a> (using the <spanClass="emphasis">CURLOPT_HTTPPOST</span> option), you must not free the list until after you've called <aclass="emphasis"href="./curl_easy_cleanup.html">curl_easy_cleanup(3)</a> for the curl handle.
<pclass="level1">See example below. <aname="RETURN"></a><h2class="nroffsh">RETURN VALUE</h2>
<pclass="level0">0 means everything was ok, non-zero means an error occurred corresponding to a CURL_FORMADD_* constant defined in <spanClass="emphasis"><curl/curl.h></span><aname="EXAMPLE"></a><h2class="nroffsh">EXAMPLE</h2>
<pclass="level0"> /* no option needed for the end marker */ curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures", CURLFORM_ARRAY, forms, CURLFORM_END); /* Add the content of a file as a normal post text value */ curl_formadd(&post, &last, CURLFORM_COPYNAME, "filecontent", CURLFORM_FILECONTENT, ".bashrc", CURLFORM_END); /* Set the form info */ curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);