corrected to match reality better
[platform/upstream/curl.git] / docs / libcurl / curl_formadd.3
1 .\" You can view this file with:
2 .\" nroff -man [file]
3 .\" $Id$
4 .\"
5 .TH curl_formadd 3 "24 June 2002" "libcurl 7.9.8" "libcurl Manual"
6 .SH NAME
7 curl_formadd - add a section to a multipart/formdata HTTP POST
8 .SH SYNOPSIS
9 .B #include <curl/curl.h>
10 .sp
11 .BI "CURLFORMcode curl_formadd(struct HttpPost ** " firstitem,
12 .BI "struct HttpPost ** " lastitem, " ...);"
13 .ad
14 .SH DESCRIPTION
15 curl_formadd() is used to append sections when building a multipart/formdata
16 HTTP POST (sometimes refered 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.
21
22 After the \fIlastitem\fP pointer follow the real arguments. (If the following
23 description confuses you, jump directly to the examples):
24
25 \fBCURLFORM_COPYNAME\fP or \fBCURLFORM_PTRNAME\fP followed by a string is used
26 for the name of the section. Optionally one may use \fBCURLFORM_NAMELENGTH\fP
27 to specify the length of the name (allowing null characters within the
28 name). All options that use the word COPY in their names copy the given
29 contents, while the ones with PTR in their names simply points to the (static)
30 data you must make sure remain until curl no longer needs it.
31
32 The options for providing values are: \fBCURLFORM_COPYCONTENTS\fP,
33 \fBCURLFORM_PTRCONTENTS\fP, \fBCURLFORM_FILE\fP, \fBCURLFORM_BUFFER\fP,
34 or \fBCURLFORM_FILECONTENT\fP followed by a char or void pointer
35 (allowed for PTRCONTENTS).
36
37 \fBCURLFORM_FILECONTENT\fP does a normal post like \fBCURLFORM_COPYCONTENTS\fP
38 but the actual value is read from the filename given as a string.
39
40 Other arguments may be \fBCURLFORM_CONTENTTYPE\fP if the user wishes to
41 specify one (for FILE if no type is given the library tries to provide the
42 correct one; for CONTENTS no Content-Type is sent in this case).
43
44 For \fBCURLFORM_PTRCONTENTS\fP or \fBCURLFORM_COPYNAME\fP the user may also
45 add \fBCURLFORM_CONTENTSLENGTH\fP followed by the length as a long (if not
46 given the library will use strlen to determine the length).
47
48 For \fBCURLFORM_FILE\fP the user may send multiple files in one section by
49 providing multiple \fBCURLFORM_FILE\fP arguments each followed by the filename
50 (and each FILE is allowed to have a CONTENTTYPE).
51
52 \fBCURLFORM_BUFFER\fP 
53 tells libcurl that a buffer is to be used to upload data instead of using a
54 file. The value of the next parameter is used as the value of the "filename"
55 parameter in the content header.
56
57 \fBCURLFORM_BUFFERPTR\fP
58 tells libcurl that the address of the next parameter is a pointer to the buffer
59 containing data to upload. The buffer containing this data must not be freed
60 until after curl_easy_cleanup is called.
61
62 \fBCURLFORM_BUFFERLENGTH\fP
63 tells libcurl that the length of the buffer to upload is the value of the
64 next parameter.
65
66 Another possibility to send options to curl_formadd() is the
67 \fBCURLFORM_ARRAY\fP option, that passes a struct curl_forms array pointer as
68 its value. Each curl_forms structure element has a CURLformoption and a char
69 pointer. The final element in the array must be a CURLFORM_END. All available
70 options can be used in an array, except the CURLFORM_ARRAY option itself!
71
72 Should you need to specify extra headers for the form POST section, use
73 \fBCURLFORM_CONTENTHEADER\fP. This takes a curl_slist prepared in the usual way
74 using \fBcurl_slist_append\fP and appends the list of headers to those Curl
75 automatically generates for \fBCURLFORM_CONTENTTYPE\fP and the content 
76 disposition. The list must exist while the POST occurs, if you free it before
77 the post completes you may experience problems.
78
79 The last argument in such an array must always be \fBCURLFORM_END\fP.
80
81 The pointers \fI*firstitem\fP and \fI*lastitem\fP should both be pointing to
82 NULL in the first call to this function. All list-data will be allocated by
83 the function itself. You must call \fIcurl_formfree\fP after the form post has
84 been done to free the resources again.
85
86 This function will copy all input data except the data pointed to by the
87 arguments after \fBCURLFORM_PTRNAME\fP and \fBCURLFORM_PTRCONTENTS\fP and keep
88 its own version of it allocated until you call \fIcurl_formfree\fP. When
89 you've passed the pointer to \fIcurl_easy_setopt\fP, you must not free the
90 list until after you've called \fIcurl_easy_cleanup\fP for the curl handle. If
91 you provide a pointer as an arguments after \fBCURLFORM_PTRNAME\fP or
92 \fBCURLFORM_PTRCONTENTS\fP you must ensure that the pointer stays valid until
93 you call \fIcurl_form_free\fP and \fIcurl_easy_cleanup\fP.
94
95 See example below.
96 .SH RETURN VALUE
97 0 means everything was ok, non-zero means an error occurred as
98 .I <curl/curl.h>
99 defines.
100 .SH EXAMPLE
101 .nf
102
103  struct HttpPost* post = NULL;
104  struct HttpPost* last = NULL;
105  char namebuffer[] = "name buffer";
106  long namelength = strlen(namebuffer);
107  char buffer[] = "test buffer";
108  char htmlbuffer[] = "<HTML>test buffer</HTML>";
109  long htmlbufferlength = strlen(htmlbuffer);
110  struct curl_forms forms[3];
111  char file1[] = "my-face.jpg";
112  char file2[] = "your-face.jpg";
113  /* add null character into htmlbuffer, to demonstrate that
114     transfers of buffers containing null characters actually work
115  */
116  htmlbuffer[8] = '\\0';
117
118  /* Add simple name/content section */
119  curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
120               CURLFORM_COPYCONTENTS, "content", CURLFORM_END); 
121
122  /* Add simple name/content/contenttype section */
123  curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
124               CURLFORM_COPYCONTENTS, "<HTML></HTML>",
125               CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
126
127  /* Add name/ptrcontent section */
128  curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
129               CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
130
131  /* Add ptrname/ptrcontent section */
132  curl_formadd(&post, &last, CURLFORM_PTRNAME, namebuffer,
133               CURLFORM_PTRCONTENTS, buffer, CURLFORM_NAMELENGTH,
134               namelength, CURLFORM_END);
135
136  /* Add name/ptrcontent/contenttype section */
137  curl_formadd(&post, &last, CURLFORM_COPYNAME, "html_code_with_hole",
138               CURLFORM_PTRCONTENTS, htmlbuffer,
139               CURLFORM_CONTENTSLENGTH, htmlbufferlength,
140               CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
141
142  /* Add simple file section */
143  curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
144               CURLFORM_FILE, "my-face.jpg", CURLFORM_END);
145
146  /* Add file/contenttype section */
147  curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
148               CURLFORM_FILE, "my-face.jpg",
149               CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END);
150
151  /* Add two file section */
152  curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures",
153               CURLFORM_FILE, "my-face.jpg",
154               CURLFORM_FILE, "your-face.jpg", CURLFORM_END);
155
156  /* Add two file section using CURLFORM_ARRAY */
157  forms[0].option = CURLFORM_FILE;
158  forms[0].value  = file1;
159  forms[1].option = CURLFORM_FILE;
160  forms[1].value  = file2;
161  forms[2].option  = CURLFORM_END;
162
163  /* Add a buffer to upload */
164  curl_formadd(&post, &last,
165               CURLFORM_COPYNAME, "name",
166               CURLFORM_BUFFER, "data",
167               CURLFORM_BUFFERPTR, record,
168               CURLFORM_BUFFERLENGTH, record_length,
169               CURLFORM_END);
170
171  /* no option needed for the end marker */
172  curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures",
173               CURLFORM_ARRAY, forms, CURLFORM_END);
174  /* Add the content of a file as a normal post text value */
175  curl_formadd(&post, &last, CURLFORM_COPYNAME, "filecontent",
176               CURLFORM_FILECONTENT, ".bashrc", CURLFORM_END);
177  /* Set the form info */
178  curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
179
180 .SH "SEE ALSO"
181 .BR curl_easy_setopt "(3), "
182 .BR curl_formparse "(3) [deprecated], "
183 .BR curl_formfree "(3)"
184 .SH BUGS
185 Surely there are some, you tell me!
186