curl_formadd.3 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. .\" *
  10. .\" * This software is licensed as described in the file COPYING, which
  11. .\" * you should have received as part of this distribution. The terms
  12. .\" * are also available at http://curl.haxx.se/docs/copyright.html.
  13. .\" *
  14. .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. .\" * copies of the Software, and permit persons to whom the Software is
  16. .\" * furnished to do so, under the terms of the COPYING file.
  17. .\" *
  18. .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. .\" * KIND, either express or implied.
  20. .\" *
  21. .\" **************************************************************************
  22. .TH curl_formadd 3 "24 June 2002" "libcurl 7.9.8" "libcurl Manual"
  23. .SH NAME
  24. curl_formadd - add a section to a multipart/formdata HTTP POST
  25. .SH SYNOPSIS
  26. .B #include <curl/curl.h>
  27. .sp
  28. .BI "CURLFORMcode curl_formadd(struct curl_httppost ** " firstitem,
  29. .BI "struct curl_httppost ** " lastitem, " ...);"
  30. .ad
  31. .SH DESCRIPTION
  32. curl_formadd() is used to append sections when building a multipart/formdata
  33. HTTP POST (sometimes referred to as RFC2388-style posts). Append one section
  34. at a time until you've added all the sections you want included and then you
  35. pass the \fIfirstitem\fP pointer as parameter to \fBCURLOPT_HTTPPOST\fP.
  36. \fIlastitem\fP is set after each \fIcurl_formadd(3)\fP call and on repeated
  37. invokes it should be left as set to allow repeated invokes to find the end of
  38. the list faster.
  39. After the \fIlastitem\fP pointer follow the real arguments.
  40. The pointers \fIfirstitem\fP and \fIlastitem\fP should both be pointing to
  41. NULL in the first call to this function. All list-data will be allocated by
  42. the function itself. You must call \fIcurl_formfree(3)\fP on the
  43. \fIfirstitem\P after the form post has been done to free the resources.
  44. Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue" header.
  45. You can disable this header with \fICURLOPT_HTTPHEADER\fP as usual.
  46. First, there are some basics you need to understand about multipart/formdata
  47. posts. Each part consists of at least a NAME and a CONTENTS part. If the part
  48. is made for file upload, there are also a stored CONTENT-TYPE and a FILENAME.
  49. Below, we'll discuss what options you use to set these properties in the
  50. parts you want to add to your post.
  51. The options listed first are for making normal parts. The options from
  52. \fICURLFORM_FILE\fP through \fICURLFORM_BUFFERLENGTH\fP are for file upload
  53. parts.
  54. .SH OPTIONS
  55. .IP CURLFORM_COPYNAME
  56. followed by a string which provides the \fIname\fP of this part. libcurl
  57. copies the string so your application doesn't need to keep it around after
  58. this function call. If the name isn't NUL-terminated, or if you'd
  59. like it to contain zero bytes, you must set its length with
  60. \fBCURLFORM_NAMELENGTH\fP. The copied data will be freed by
  61. \fIcurl_formfree(3)\fP.
  62. .IP CURLFORM_PTRNAME
  63. followed by a string which provides the \fIname\fP of this part. libcurl
  64. will use the pointer and refer to the data in your application, so you
  65. must make sure it remains until curl no longer needs it. If the name
  66. isn't NUL-terminated, or if you'd like it to contain zero
  67. bytes, you must set its length with \fBCURLFORM_NAMELENGTH\fP.
  68. .IP CURLFORM_COPYCONTENTS
  69. followed by a pointer to the contents of this part, the actual data
  70. to send away. libcurl copies the provided data, so your application doesn't
  71. need to keep it around after this function call. If the data isn't null
  72. terminated, or if you'd like it to contain zero bytes, you must
  73. set the length of the name with \fBCURLFORM_CONTENTSLENGTH\fP. The copied
  74. data will be freed by \fIcurl_formfree(3)\fP.
  75. .IP CURLFORM_PTRCONTENTS
  76. followed by a pointer to the contents of this part, the actual data
  77. to send away. libcurl will use the pointer and refer to the data in your
  78. application, so you must make sure it remains until curl no longer needs it.
  79. If the data isn't NUL-terminated, or if you'd like it to contain zero bytes,
  80. you must set its length with \fBCURLFORM_CONTENTSLENGTH\fP.
  81. .IP CURLFORM_CONTENTSLENGTH
  82. followed by a long giving the length of the contents. Note that for
  83. \fICURLFORM_STREAM\fP contents, this option is mandatory.
  84. .IP CURLFORM_FILECONTENT
  85. followed by a filename, causes that file to be read and its contents used
  86. as data in this part. This part does \fInot\fP automatically become a file
  87. upload part simply because its data was read from a file.
  88. .IP CURLFORM_FILE
  89. followed by a filename, makes this part a file upload part. It sets the
  90. \fIfilename\fP field to the basename of the provided filename, it reads the
  91. contents of the file and passes them as data and sets the content-type if the
  92. given file match one of the internally known file extensions. For
  93. \fBCURLFORM_FILE\fP the user may send one or more files in one part by
  94. providing multiple \fBCURLFORM_FILE\fP arguments each followed by the filename
  95. (and each \fICURLFORM_FILE\fP is allowed to have a
  96. \fICURLFORM_CONTENTTYPE\fP).
  97. .IP CURLFORM_CONTENTTYPE
  98. is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a
  99. string which provides the content-type for this part, possibly instead of an
  100. internally chosen one.
  101. .IP CURLFORM_FILENAME
  102. is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a
  103. string, it tells libcurl to use the given string as the \fIfilename\fP in the
  104. file upload part instead of the actual file name.
  105. .IP CURLFORM_BUFFER
  106. is used for custom file upload parts without use of \fICURLFORM_FILE\fP. It
  107. tells libcurl that the file contents are already present in a buffer. The
  108. parameter is a string which provides the \fIfilename\fP field in the content
  109. header.
  110. .IP CURLFORM_BUFFERPTR
  111. is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a pointer
  112. to the buffer to be uploaded. This buffer must not be freed until after
  113. \fIcurl_easy_cleanup(3)\fP is called. You must also use
  114. \fICURLFORM_BUFFERLENGTH\fP to set the number of bytes in the buffer.
  115. .IP CURLFORM_BUFFERLENGTH
  116. is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a
  117. long which gives the length of the buffer.
  118. .IP CURLFORM_STREAM
  119. Tells libcurl to use the \fICURLOPT_READFUNCTION\fP callback to get data. The
  120. parameter you pass to \fICURLFORM_STREAM\fP is the pointer passed on to the
  121. read callback's fourth argument. If you want the part to look like a file
  122. upload one, set the \fICURLFORM_FILENAME\fP parameter as well. Note that when
  123. using \fICURLFORM_STREAM\fP, \fICURLFORM_CONTENTSLENGTH\fP must also be set
  124. with the total expected length of the part. (Option added in libcurl 7.18.2)
  125. .IP CURLFORM_ARRAY
  126. Another possibility to send options to curl_formadd() is the
  127. \fBCURLFORM_ARRAY\fP option, that passes a struct curl_forms array pointer as
  128. its value. Each curl_forms structure element has a CURLformoption and a char
  129. pointer. The final element in the array must be a CURLFORM_END. All available
  130. options can be used in an array, except the CURLFORM_ARRAY option itself! The
  131. last argument in such an array must always be \fBCURLFORM_END\fP.
  132. .IP CURLFORM_CONTENTHEADER
  133. specifies extra headers for the form POST section. This takes a curl_slist
  134. prepared in the usual way using \fBcurl_slist_append\fP and appends the list
  135. of headers to those libcurl automatically generates. The list must exist while
  136. the POST occurs, if you free it before the post completes you may experience
  137. problems.
  138. When you've passed the HttpPost pointer to \fIcurl_easy_setopt(3)\fP (using
  139. the \fICURLOPT_HTTPPOST\fP option), you must not free the list until after
  140. you've called \fIcurl_easy_cleanup(3)\fP for the curl handle.
  141. See example below.
  142. .SH RETURN VALUE
  143. 0 means everything was ok, non-zero means an error occurred corresponding
  144. to a CURL_FORMADD_* constant defined in
  145. .I <curl/curl.h>
  146. .SH EXAMPLE
  147. .nf
  148. struct curl_httppost* post = NULL;
  149. struct curl_httppost* last = NULL;
  150. char namebuffer[] = "name buffer";
  151. long namelength = strlen(namebuffer);
  152. char buffer[] = "test buffer";
  153. char htmlbuffer[] = "<HTML>test buffer</HTML>";
  154. long htmlbufferlength = strlen(htmlbuffer);
  155. struct curl_forms forms[3];
  156. char file1[] = "my-face.jpg";
  157. char file2[] = "your-face.jpg";
  158. /* add null character into htmlbuffer, to demonstrate that
  159. transfers of buffers containing null characters actually work
  160. */
  161. htmlbuffer[8] = '\\0';
  162. /* Add simple name/content section */
  163. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
  164. CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
  165. /* Add simple name/content/contenttype section */
  166. curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
  167. CURLFORM_COPYCONTENTS, "<HTML></HTML>",
  168. CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
  169. /* Add name/ptrcontent section */
  170. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
  171. 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);
  176. /* Add name/ptrcontent/contenttype section */
  177. curl_formadd(&post, &last, CURLFORM_COPYNAME, "html_code_with_hole",
  178. CURLFORM_PTRCONTENTS, htmlbuffer,
  179. CURLFORM_CONTENTSLENGTH, htmlbufferlength,
  180. CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
  181. /* Add simple file section */
  182. curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
  183. CURLFORM_FILE, "my-face.jpg", CURLFORM_END);
  184. /* Add file/contenttype section */
  185. curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
  186. CURLFORM_FILE, "my-face.jpg",
  187. CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END);
  188. /* Add two file section */
  189. curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures",
  190. CURLFORM_FILE, "my-face.jpg",
  191. CURLFORM_FILE, "your-face.jpg", CURLFORM_END);
  192. /* Add two file section using CURLFORM_ARRAY */
  193. forms[0].option = CURLFORM_FILE;
  194. forms[0].value = file1;
  195. forms[1].option = CURLFORM_FILE;
  196. forms[1].value = file2;
  197. forms[2].option = CURLFORM_END;
  198. /* Add a buffer to upload */
  199. curl_formadd(&post, &last,
  200. CURLFORM_COPYNAME, "name",
  201. CURLFORM_BUFFER, "data",
  202. CURLFORM_BUFFERPTR, record,
  203. CURLFORM_BUFFERLENGTH, record_length,
  204. CURLFORM_END);
  205. /* no option needed for the end marker */
  206. curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures",
  207. CURLFORM_ARRAY, forms, CURLFORM_END);
  208. /* Add the content of a file as a normal post text value */
  209. curl_formadd(&post, &last, CURLFORM_COPYNAME, "filecontent",
  210. CURLFORM_FILECONTENT, ".bashrc", CURLFORM_END);
  211. /* Set the form info */
  212. curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
  213. .SH "SEE ALSO"
  214. .BR curl_easy_setopt "(3), "
  215. .BR curl_formfree "(3)"