curl_formadd.3 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 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 https://curl.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. .\" * SPDX-License-Identifier: curl
  22. .\" *
  23. .\" **************************************************************************
  24. .TH curl_formadd 3 "24 June 2002" "libcurl" "libcurl"
  25. .SH NAME
  26. curl_formadd - add a section to a multipart form POST
  27. .SH SYNOPSIS
  28. .nf
  29. #include <curl/curl.h>
  30. CURLFORMcode curl_formadd(struct curl_httppost **firstitem,
  31. struct curl_httppost **lastitem, ...);
  32. .fi
  33. .SH DESCRIPTION
  34. \fBThis function is deprecated.\fP Use \fIcurl_mime_init(3)\fP instead.
  35. curl_formadd() is used to append sections when building a multipart form
  36. post. Append one section at a time until you have added all the sections you
  37. want included and then you pass the \fIfirstitem\fP pointer as parameter to
  38. \fICURLOPT_HTTPPOST(3)\fP. \fIlastitem\fP is set after each
  39. \fIcurl_formadd(3)\fP call and on repeated invokes it should be left as set to
  40. allow repeated invokes to find the end of the list faster.
  41. After the \fIlastitem\fP pointer follow the real arguments.
  42. The pointers \fIfirstitem\fP and \fIlastitem\fP should both be pointing to
  43. NULL in the first call to this function. All list-data will be allocated by
  44. the function itself. You must call \fIcurl_formfree(3)\fP on the
  45. \fIfirstitem\fP after the form post has been done to free the resources.
  46. Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue" header.
  47. You can disable this header with \fICURLOPT_HTTPHEADER(3)\fP as usual.
  48. First, there are some basics you need to understand about multipart form
  49. posts. Each part consists of at least a NAME and a CONTENTS part. If the part
  50. is made for file upload, there are also a stored CONTENT-TYPE and a FILENAME.
  51. Below, we will discuss what options you use to set these properties in the
  52. parts you want to add to your post.
  53. The options listed first are for making normal parts. The options from
  54. \fICURLFORM_FILE\fP through \fICURLFORM_BUFFERLENGTH\fP are for file upload
  55. parts.
  56. .SH OPTIONS
  57. .IP CURLFORM_COPYNAME
  58. followed by a string which provides the \fIname\fP of this part. libcurl
  59. copies the string so your application does not need to keep it around after
  60. this function call. If the name is not null-terminated, you must set its
  61. length with \fBCURLFORM_NAMELENGTH\fP. The \fIname\fP is not allowed to
  62. contain zero-valued bytes. The copied data will be freed by
  63. \fIcurl_formfree(3)\fP.
  64. .IP CURLFORM_PTRNAME
  65. followed by a string which provides the \fIname\fP of this part. libcurl
  66. will use the pointer and refer to the data in your application, so you
  67. must make sure it remains until curl no longer needs it. If the name
  68. is not null-terminated, you must set its length with \fBCURLFORM_NAMELENGTH\fP.
  69. The \fIname\fP is not allowed to contain zero-valued bytes.
  70. .IP CURLFORM_COPYCONTENTS
  71. followed by a pointer to the contents of this part, the actual data
  72. to send away. libcurl copies the provided data, so your application does not
  73. need to keep it around after this function call. If the data is not null
  74. terminated, or if you would like it to contain zero bytes, you must
  75. set the length of the name with \fBCURLFORM_CONTENTSLENGTH\fP. The copied
  76. data will be freed by \fIcurl_formfree(3)\fP.
  77. .IP CURLFORM_PTRCONTENTS
  78. followed by a pointer to the contents of this part, the actual data to send
  79. away. libcurl will use the pointer and refer to the data in your application,
  80. so you must make sure it remains until curl no longer needs it. If the data
  81. is not null-terminated, or if you would like it to contain zero bytes, you
  82. must set its length with \fBCURLFORM_CONTENTSLENGTH\fP.
  83. .IP CURLFORM_CONTENTLEN
  84. followed by a curl_off_t value giving the length of the contents. Note that
  85. for \fICURLFORM_STREAM\fP contents, this option is mandatory.
  86. If you pass a 0 (zero) for this option, libcurl will instead do a strlen() on
  87. the contents to figure out the size. If you really want to send a zero byte
  88. content then you must make sure strlen() on the data pointer returns zero.
  89. (Option added in 7.46.0)
  90. .IP CURLFORM_CONTENTSLENGTH
  91. (This option is deprecated. Use \fICURLFORM_CONTENTLEN\fP instead!)
  92. followed by a long giving the length of the contents. Note that for
  93. \fICURLFORM_STREAM\fP contents, this option is mandatory.
  94. If you pass a 0 (zero) for this option, libcurl will instead do a strlen() on
  95. the contents to figure out the size. If you really want to send a zero byte
  96. content then you must make sure strlen() on the data pointer returns zero.
  97. .IP CURLFORM_FILECONTENT
  98. followed by a filename, causes that file to be read and its contents used
  99. as data in this part. This part does \fInot\fP automatically become a file
  100. upload part simply because its data was read from a file.
  101. The specified file needs to kept around until the associated transfer is done.
  102. .IP CURLFORM_FILE
  103. followed by a filename, makes this part a file upload part. It sets the
  104. \fIfilename\fP field to the basename of the provided filename, it reads the
  105. contents of the file and passes them as data and sets the content-type if the
  106. given file match one of the internally known file extensions. For
  107. \fBCURLFORM_FILE\fP the user may send one or more files in one part by
  108. providing multiple \fBCURLFORM_FILE\fP arguments each followed by the filename
  109. (and each \fICURLFORM_FILE\fP is allowed to have a
  110. \fICURLFORM_CONTENTTYPE\fP).
  111. The given upload file has to exist in its full in the file system already when
  112. the upload starts, as libcurl needs to read the correct file size beforehand.
  113. The specified file needs to kept around until the associated transfer is done.
  114. .IP CURLFORM_CONTENTTYPE
  115. is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a
  116. string which provides the content-type for this part, possibly instead of an
  117. internally chosen one.
  118. .IP CURLFORM_FILENAME
  119. is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a
  120. string, it tells libcurl to use the given string as the \fIfilename\fP in the
  121. file upload part instead of the actual file name.
  122. .IP CURLFORM_BUFFER
  123. is used for custom file upload parts without use of \fICURLFORM_FILE\fP. It
  124. tells libcurl that the file contents are already present in a buffer. The
  125. parameter is a string which provides the \fIfilename\fP field in the content
  126. header.
  127. .IP CURLFORM_BUFFERPTR
  128. is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a pointer
  129. to the buffer to be uploaded. This buffer must not be freed until after
  130. \fIcurl_easy_cleanup(3)\fP is called. You must also use
  131. \fICURLFORM_BUFFERLENGTH\fP to set the number of bytes in the buffer.
  132. .IP CURLFORM_BUFFERLENGTH
  133. is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a
  134. long which gives the length of the buffer.
  135. .IP CURLFORM_STREAM
  136. Tells libcurl to use the \fICURLOPT_READFUNCTION(3)\fP callback to get
  137. data. The parameter you pass to \fICURLFORM_STREAM\fP is the pointer passed on
  138. to the read callback's fourth argument. If you want the part to look like a
  139. file upload one, set the \fICURLFORM_FILENAME\fP parameter as well. Note that
  140. when using \fICURLFORM_STREAM\fP, \fICURLFORM_CONTENTSLENGTH\fP must also be
  141. set with the total expected length of the part unless the formpost is sent
  142. chunked encoded. (Option added in libcurl 7.18.2)
  143. .IP CURLFORM_ARRAY
  144. Another possibility to send options to curl_formadd() is the
  145. \fBCURLFORM_ARRAY\fP option, that passes a struct curl_forms array pointer as
  146. its value. Each curl_forms structure element has a \fICURLformoption\fP and a
  147. char pointer. The final element in the array must be a CURLFORM_END. All
  148. available options can be used in an array, except the CURLFORM_ARRAY option
  149. itself. The last argument in such an array must always be \fBCURLFORM_END\fP.
  150. .IP CURLFORM_CONTENTHEADER
  151. specifies extra headers for the form POST section. This takes a curl_slist
  152. prepared in the usual way using \fBcurl_slist_append\fP and appends the list
  153. of headers to those libcurl automatically generates. The list must exist while
  154. the POST occurs, if you free it before the post completes you may experience
  155. problems.
  156. When you have passed the \fIstruct curl_httppost\fP pointer to
  157. \fIcurl_easy_setopt(3)\fP (using the \fICURLOPT_HTTPPOST(3)\fP option), you
  158. must not free the list until after you have called \fIcurl_easy_cleanup(3)\fP
  159. for the curl handle.
  160. See example below.
  161. .SH EXAMPLE
  162. .nf
  163. struct curl_httppost *post = NULL;
  164. struct curl_httppost *last = NULL;
  165. char namebuffer[] = "name buffer";
  166. long namelength = strlen(namebuffer);
  167. char buffer[] = "test buffer";
  168. char htmlbuffer[] = "<HTML>test buffer</HTML>";
  169. long htmlbufferlength = strlen(htmlbuffer);
  170. struct curl_forms forms[3];
  171. char file1[] = "my-face.jpg";
  172. char file2[] = "your-face.jpg";
  173. /* add null character into htmlbuffer, to demonstrate that
  174. transfers of buffers containing null characters actually work
  175. */
  176. htmlbuffer[8] = '\\0';
  177. /* Add simple name/content section */
  178. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
  179. CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
  180. /* Add simple name/content/contenttype section */
  181. curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
  182. CURLFORM_COPYCONTENTS, "<HTML></HTML>",
  183. CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
  184. /* Add name/ptrcontent section */
  185. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
  186. CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
  187. /* Add ptrname/ptrcontent section */
  188. curl_formadd(&post, &last, CURLFORM_PTRNAME, namebuffer,
  189. CURLFORM_PTRCONTENTS, buffer, CURLFORM_NAMELENGTH,
  190. namelength, CURLFORM_END);
  191. /* Add name/ptrcontent/contenttype section */
  192. curl_formadd(&post, &last, CURLFORM_COPYNAME, "html_code_with_hole",
  193. CURLFORM_PTRCONTENTS, htmlbuffer,
  194. CURLFORM_CONTENTSLENGTH, htmlbufferlength,
  195. CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
  196. /* Add simple file section */
  197. curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
  198. CURLFORM_FILE, "my-face.jpg", CURLFORM_END);
  199. /* Add file/contenttype section */
  200. curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
  201. CURLFORM_FILE, "my-face.jpg",
  202. CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END);
  203. /* Add two file section */
  204. curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures",
  205. CURLFORM_FILE, "my-face.jpg",
  206. CURLFORM_FILE, "your-face.jpg", CURLFORM_END);
  207. /* Add two file section using CURLFORM_ARRAY */
  208. forms[0].option = CURLFORM_FILE;
  209. forms[0].value = file1;
  210. forms[1].option = CURLFORM_FILE;
  211. forms[1].value = file2;
  212. forms[2].option = CURLFORM_END;
  213. /* Add a buffer to upload */
  214. curl_formadd(&post, &last,
  215. CURLFORM_COPYNAME, "name",
  216. CURLFORM_BUFFER, "data",
  217. CURLFORM_BUFFERPTR, record,
  218. CURLFORM_BUFFERLENGTH, record_length,
  219. CURLFORM_END);
  220. /* no option needed for the end marker */
  221. curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures",
  222. CURLFORM_ARRAY, forms, CURLFORM_END);
  223. /* Add the content of a file as a normal post text value */
  224. curl_formadd(&post, &last, CURLFORM_COPYNAME, "filecontent",
  225. CURLFORM_FILECONTENT, ".bashrc", CURLFORM_END);
  226. /* Set the form info */
  227. curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
  228. .SH AVAILABILITY
  229. Deprecated in 7.56.0. Before this release, field names were allowed to
  230. contain zero-valued bytes. The pseudo-filename "-" to read stdin is
  231. discouraged although still supported, but data is not read before being
  232. actually sent: the effective data size can then not be automatically
  233. determined, resulting in a chunked encoding transfer. Backslashes and
  234. double quotes in field and file names are now escaped before transmission.
  235. .SH RETURN VALUE
  236. 0 means everything was OK, non-zero means an error occurred corresponding
  237. to a CURL_FORMADD_* constant defined in
  238. .I <curl/curl.h>
  239. .SH "SEE ALSO"
  240. .BR curl_easy_setopt "(3),"
  241. .BR curl_formfree "(3),"
  242. .BR curl_mime_init "(3)"