Libcurl-curl_formadd () (unfinished, to be continued)

Source: Internet
Author: User
Tags form post http post
Curl libcurl APICurl_formadd

Curl_formadd.3 -- man page

Namecurl_formadd-Add a section to a multipart/formdata HTTP postcurl_formadd ---- used to add parts for multipart/formdata http post requests. Note: What is multipart/form-data? Multipart/form-data is a method for uploading files. Multipart/form-data is actually the way the browser uploads files using forms. The most common situation is: when writing an email, add attachments to the email. attachments are usually added using forms, that is, uploaded to the server in the multipart/form-data format. Synopsis

# Include <curl/curl. h>

Curlformcode curl_formadd (struct curl_httppost ** firstitem, struct curl_httppost ** lastitem ,...);

Description

Curl_formadd () is used to append sections when building a multipart/formdata http post (sometimes referred to as RFC 2388-style posts). append one section
At a time until you 've added all the sections you want defined ded and then you pass the firstitem pointer as parameter to curlopt_httppost. lastitem is
Set after each curl_formadd (3) Call and on repeated invokes it shoshould be left as set to allow repeated invokes to find the end of the list faster.

The curl_formadd () function is used to add the post option when creating multipart/formdata http post. Add a post option at a time until all your options are added. Then you pass the first pointer parameter firstitem as the parameter of curlopt_httppost.

The parameter lastitem must be set after each call of curl_formadd. because of repeated calls, this parameter must be set so that the end of the linked list can be found during repeated calls.

After the lastitem pointer follow the real arguments.

The parameter lastitem is followed by the real parameter.

The pointers firstitem and lastitem shoshould 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 curl_formfree (3) on the firstitemp after the form post has been done to free the resources.

When this function is called for the first time, the firstitem and lastitem pointers must point to null. All list-data is allocated by the function itself. After form post is complete, you must call curl_formfree (firstitem) to release the resource.

Using post with HTTP 1.1 implies the use of a "exact CT: 100-continue" header. you can disable this header with curlopt_httpheader as usual. the use of post in http1.1 implies the use of the "Expect CT: 100-continue" header. You can setCurlopt_httpheaderTo disable "CT:
100-Use of the continue header. 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. first, you need to understand some basic knowledge about multipart/formdata post. Each part contains at least one name and one content part. If this part is used to upload files, you can also have Content-Type and filename information. (Translator's note: if Wireshark is used to capture packets to view the POST request information when a file is uploaded using a fast-loading network disk, we can find that there is an extra mime segment, and the information is as follows ).

The options listed first are for making normal parts. The options from curlform_file through curlform_bufferlength are
For File Upload parts.

Options

 

Curlform_copyname

Followed by a string which provides the name 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 curlform_namelength. The copied data will be freed by curl_formfree (3 ).

Curlform_ptrname

Followed by a string which provides the name 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 curlform_namelength.

Curlform_copycontents

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 und 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 curlform_contentslength. The copied data will be freed by curl_formfree (3 ).

Curlform_ptrcontents

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 curlform_contentslength.

Curlform_contentslength

Followed by a long giving the length of the contents. Note that for curlform_stream contents, this option is mandatory.

Curlform_filecontent

Followed by a filename, causes that file to be read and its contents used as data in this part. This part does not automatically become a file upload part simply because its data was read from
A file.

Curlform_file

Followed by a filename, makes this Part A File Upload part. It sets the filename 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 curlform_file the user may send one or more files in one
Part by providing multiple curlform_file arguments each followed by the filename (and each curlform_file is
Allowed To Have A curlform_contenttype ).

Curlform_file

The following parameter is the file name. curlform_file uses this part of content as the upload part. Set filename as the basename of the provided file name. Read the data in the file and pass them as data. If the given file matches the known file type, the content-type is also set. For curlform_file, you can send one or more files by providing multiple curlform_file parameters.

Curlform_contenttype

Is used in combination with curlform_file. Followed by a pointer to a string which provides the Content-Type for this part, possibly
Instead of an internally chosen one.

Curlform_filename

Is used in combination with curlform_file. Followed
By a pointer to a string, it tells libcurl to use the given string as the filename in the file upload part instead of the actual file name.

Curlform_buffer

Is used for custom file upload parts without use of curlform_file. It tells libcurl that the file contents are already present
In a buffer. The parameter is a string which provides the filename field in the Content Header.

Curlform_bufferptr

Is used in combination with curlform_buffer. The parameter is a pointer to the buffer to be uploaded. This buffer must not
Be freed until after curl_easy_cleanup (3) is called. You must also use curlform_bufferlength
Set the number of bytes in the buffer.

Curlform_bufferlength

Is used in combination with curlform_buffer. The parameter is a long which gives the length of the buffer.

Curlform_stream

Tells libcurl to use the curlopt_readfunction callback to get data. The parameter you pass to curlform_stream 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 curlform_filename Parameter
As well. Note that when using curlform_stream, curlform_contentslength must
Also be set with the total expected length of the part. (option added in libcurl 7.18.2)

Curlform_array

Another possibility to send options to curl_formadd () is the curlform_array 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, cannot the curlform_array option itself! The last argument in such
An array must always be curlform_end.

Curlform_contentheader

Specifies extra headers for the form post Section. This takes a curl_slist prepared in the usual way using curl_slist_append 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.

When you 've passed the httppost pointer to curl_easy_setopt (3) (using the curlopt_httppost option ),
You must not free the list until after you 've called curl_easy_cleanup (3) for the curl handle.

See example below.

Return Value

0 means everything was OK, non-zero means an error occurred corresponding to a curl_formadd _ * constant defined in <curl/curl. h>

Example

 

 

Struct curl_httppost * post = NULL;

Struct curl_httppost * Last = NULL;

Char namebuffer [] = "Name Buffer ";

Long namelength = strlen (namebuffer );

Char buffer [] = "test buffer ";

Char htmlbuffer [] = "<HTML> test buffer

Long htmlbufferlength = strlen (htmlbuffer );

Struct curl_forms forms [3];

Char file1 [] = "my-face.jpg ";

Char file2 [] = "your-face.jpg ";

/* Add null character into htmlbuffer, to demonstrate that transfers of buffers containing null characters actually work */

Htmlbuffer [8] = '\ 0 ';

/* Add simple name/content section */

Curl_formadd (& post, & last, curlform_copyname, "name", curlform_copycontents, "content", curlform_end );

/* Add simple name/content/contenttype Section */

Curl_formadd (& post, & last, curlform_copyname, "htmlcode", curlform_copycontents, "<HTML>

/* Add name/ptrcontent Section */

Curl_formadd (& post, & last, curlform_copyname, "name_for_ptrcontent", curlform_ptrcontents, buffer, curlform_end );

/* Add ptrname/ptrcontent Section */

Curl_formadd (& post, & last, curlform_ptrname, namebuffer, curlform_ptrcontents, buffer, curlform_namelength, namelength, curlform_end );

/* Add name/ptrcontent/contenttype Section */

Curl_formadd (& post, & last, curlform_copyname, "html_code_with_hole", expiration, htmlbuffer, expiration, htmlbufferlength, curlform_contenttype, "text/html", curlform_end );

/* Add simple file section */curl_formadd (& post, & last, curlform_copyname, "picture", curlform_file, "my-face.jpg", curlform_end );

/* Add file/contenttype Section */

Curl_formadd (& post, & last, curlform_copyname, "picture", curlform_file, "my-face.jpg", curlform_contenttype, "image/JPEG", curlform_end );

/* Add two FILE Section */

Curl_formadd (& post, & last, curlform_copyname, "pictures", curlform_file, "my-face.jpg", curlform_file, "your-face.jpg", curlform_end );

/* Add two file section using curlform_array */

Forms [0]. option = curlform_file; forms [0]. value = file1; forms [1]. option = curlform_file; forms [1]. value = file2; forms [2]. option = curlform_end;

/* Add a buffer to upload */

Curl_formadd (& post, & last, curlform_copyname, "name", curlform_buffer, "data", curlform_bufferptr, record, curlform_bufferlength, record_length, curlform_end );

/* 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 );

See also

Curl_easy_setopt (3) curl_formfree (3)

This html page was made with roffit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.