curl_formadd.3 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. .\" generated by cd2nroff 0.1 from curl_formadd.md
  2. .TH curl_formadd 3 "2025-01-17" libcurl
  3. .SH NAME
  4. curl_formadd \- add a section to a multipart form POST
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLFORMcode curl_formadd(struct curl_httppost **firstitem,
  9. struct curl_httppost **lastitem, ...);
  10. .fi
  11. .SH DESCRIPTION
  12. \fBThis function is deprecated.\fP Use \fIcurl_mime_init(3)\fP instead.
  13. curl_formadd() is used to append sections when building a multipart form
  14. post. Append one section at a time until you have added all the sections you
  15. want included and then you pass the \fIfirstitem\fP pointer as parameter to
  16. \fICURLOPT_HTTPPOST(3)\fP. \fIlastitem\fP is set after each \fIcurl_formadd(3)\fP call and
  17. on repeated invokes it should be left as set to allow repeated invokes to find
  18. the end of the list faster.
  19. After the \fIlastitem\fP pointer follow the real arguments.
  20. The pointers \fIfirstitem\fP and \fIlastitem\fP should both be pointing to
  21. NULL in the first call to this function. All list\-data is allocated by the
  22. function itself. You must call \fIcurl_formfree(3)\fP on the \fIfirstitem\fP
  23. after the form post has been done to free the resources.
  24. Using POST with HTTP 1.1 implies the use of a "Expect: 100\-continue" header.
  25. You can disable this header with \fICURLOPT_HTTPHEADER(3)\fP as usual.
  26. First, there are some basics you need to understand about multipart form
  27. posts. Each part consists of at least a NAME and a CONTENTS part. If the part
  28. is made for file upload, there are also a stored CONTENT\-TYPE and a FILENAME.
  29. Below, we discuss what options you use to set these properties in the parts
  30. you want to add to your post.
  31. The options listed first are for making normal parts. The options from
  32. \fICURLFORM_FILE\fP through \fICURLFORM_BUFFERLENGTH\fP are for file upload
  33. parts.
  34. .SH OPTIONS
  35. .IP CURLFORM_COPYNAME
  36. followed by a string which provides the \fIname\fP of this part. libcurl
  37. copies the string so your application does not need to keep it around after
  38. this function call. If the name is not null\-terminated, you must set its
  39. length with \fBCURLFORM_NAMELENGTH\fP. The \fIname\fP is not allowed to
  40. contain zero\-valued bytes. The copied data is freed by \fIcurl_formfree(3)\fP.
  41. .IP CURLFORM_PTRNAME
  42. followed by a string which provides the \fIname\fP of this part. libcurl uses the
  43. pointer and refer to the data in your application, so you must make sure it
  44. remains until curl no longer needs it. If the name is not null\-terminated, you
  45. must set its length with \fBCURLFORM_NAMELENGTH\fP. The \fIname\fP is not allowed to
  46. contain zero\-valued bytes.
  47. .IP CURLFORM_COPYCONTENTS
  48. followed by a pointer to the contents of this part, the actual data to send
  49. away. libcurl copies the provided data, so your application does not need to
  50. keep it around after this function call. If the data is not null terminated,
  51. or if you would like it to contain zero bytes, you must set the length of the
  52. name with \fBCURLFORM_CONTENTSLENGTH\fP. The copied data is freed by
  53. \fIcurl_formfree(3)\fP.
  54. .IP CURLFORM_PTRCONTENTS
  55. followed by a pointer to the contents of this part, the actual data to send
  56. away. libcurl uses the pointer and refer to the data in your application, so
  57. you must make sure it remains until curl no longer needs it. If the data is
  58. not null\-terminated, or if you would like it to contain zero bytes, you must
  59. set its length with \fBCURLFORM_CONTENTSLENGTH\fP.
  60. .IP CURLFORM_CONTENTLEN
  61. followed by a curl_off_t value giving the length of the contents. Note that
  62. for \fICURLFORM_STREAM\fP contents, this option is mandatory.
  63. If you pass a 0 (zero) for this option, libcurl calls strlen() on the contents
  64. to figure out the size. If you really want to send a zero byte content then
  65. you must make sure strlen() on the data pointer returns zero.
  66. (Option added in 7.46.0)
  67. .IP CURLFORM_CONTENTSLENGTH
  68. (This option is deprecated. Use \fICURLFORM_CONTENTLEN\fP instead!)
  69. followed by a long giving the length of the contents. Note that for
  70. \fICURLFORM_STREAM\fP contents, this option is mandatory.
  71. If you pass a 0 (zero) for this option, libcurl calls strlen() on the contents
  72. to figure out the size. If you really want to send a zero byte content then
  73. you must make sure strlen() on the data pointer returns zero.
  74. .IP CURLFORM_FILECONTENT
  75. followed by a filename, causes that file to be read and its contents used
  76. as data in this part. This part does \fInot\fP automatically become a file
  77. upload part simply because its data was read from a file.
  78. The specified file needs to kept around until the associated transfer is done.
  79. .IP CURLFORM_FILE
  80. followed by a filename, makes this part a file upload part. It sets the
  81. \fIfilename\fP field to the basename of the provided filename, it reads the
  82. contents of the file and passes them as data and sets the content\-type if the
  83. given file match one of the internally known file extensions. For
  84. \fBCURLFORM_FILE\fP the user may send one or more files in one part by
  85. providing multiple \fBCURLFORM_FILE\fP arguments each followed by the filename
  86. (and each \fICURLFORM_FILE\fP is allowed to have a
  87. \fICURLFORM_CONTENTTYPE\fP).
  88. The given upload file has to exist in its full in the file system already when
  89. the upload starts, as libcurl needs to read the correct file size beforehand.
  90. The specified file needs to kept around until the associated transfer is done.
  91. .IP CURLFORM_CONTENTTYPE
  92. is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a
  93. string which provides the content\-type for this part, possibly instead of an
  94. internally chosen one.
  95. .IP CURLFORM_FILENAME
  96. is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a
  97. string, it tells libcurl to use the given string as the \fIfilename\fP in the file
  98. upload part instead of the actual filename.
  99. .IP CURLFORM_BUFFER
  100. is used for custom file upload parts without use of \fICURLFORM_FILE\fP. It
  101. tells libcurl that the file contents are already present in a buffer. The
  102. parameter is a string which provides the \fIfilename\fP field in the content
  103. header.
  104. .IP CURLFORM_BUFFERPTR
  105. is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a pointer
  106. to the buffer to be uploaded. This buffer must not be freed until after
  107. \fIcurl_easy_cleanup(3)\fP is called. You must also use
  108. \fICURLFORM_BUFFERLENGTH\fP to set the number of bytes in the buffer.
  109. .IP CURLFORM_BUFFERLENGTH
  110. is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a
  111. long which gives the length of the buffer.
  112. .IP CURLFORM_STREAM
  113. Tells libcurl to use the \fICURLOPT_READFUNCTION(3)\fP callback to get
  114. data. The parameter you pass to \fICURLFORM_STREAM\fP is the pointer passed on
  115. to the read callback\(aqs fourth argument. If you want the part to look like a
  116. file upload one, set the \fICURLFORM_FILENAME\fP parameter as well. Note that
  117. when using \fICURLFORM_STREAM\fP, \fICURLFORM_CONTENTSLENGTH\fP must also be
  118. set with the total expected length of the part unless the formpost is sent
  119. chunked encoded. (Option added in libcurl 7.18.2)
  120. .IP CURLFORM_ARRAY
  121. Another possibility to send options to curl_formadd() is the
  122. \fBCURLFORM_ARRAY\fP option, that passes a struct curl_forms array pointer as
  123. its value. Each curl_forms structure element has a \fICURLformoption\fP and a
  124. char pointer. The final element in the array must be a CURLFORM_END. All
  125. available options can be used in an array, except the CURLFORM_ARRAY option
  126. itself. The last argument in such an array must always be \fBCURLFORM_END\fP.
  127. .IP CURLFORM_CONTENTHEADER
  128. specifies extra headers for the form POST section. This takes a curl_slist
  129. prepared in the usual way using \fBcurl_slist_append\fP and appends the list
  130. of headers to those libcurl automatically generates. The list must exist while
  131. the POST occurs, if you free it before the post completes you may experience
  132. problems.
  133. When you have passed the \fIstruct curl_httppost\fP pointer to
  134. \fIcurl_easy_setopt(3)\fP (using the \fICURLOPT_HTTPPOST(3)\fP option), you
  135. must not free the list until after you have called \fIcurl_easy_cleanup(3)\fP
  136. for the curl handle.
  137. See example below.
  138. .SH PROTOCOLS
  139. This functionality affects http only
  140. .SH EXAMPLE
  141. .nf
  142. #include <string.h> /* for strlen */
  143. static const char record[]="data in a buffer";
  144. int main(void)
  145. {
  146. CURL *curl = curl_easy_init();
  147. if(curl) {
  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, sizeof(record),
  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. curl_easy_perform(curl);
  214. curl_easy_cleanup(curl);
  215. curl_formfree(post);
  216. }
  217. }
  218. .fi
  219. .SH DEPRECATED
  220. Deprecated in 7.56.0. Before this release, field names were allowed to contain
  221. zero\-valued bytes. The pseudo\-filename "\-" to read stdin is discouraged
  222. although still supported, but data is not read before being actually sent: the
  223. effective data size can then not be automatically determined, resulting in a
  224. chunked encoding transfer. Backslashes and double quotes in field and
  225. filenames are now escaped before transmission.
  226. .SH AVAILABILITY
  227. Added in curl 7.1
  228. .SH RETURN VALUE
  229. 0 means everything was OK, non\-zero means an error occurred corresponding to a
  230. CURL_FORMADD_* constant defined in \fI<curl/curl.h>\fP.
  231. .SH SEE ALSO
  232. .BR curl_easy_setopt (3),
  233. .BR curl_formfree (3),
  234. .BR curl_mime_init (3)