curl_formadd.3 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. .\" You can view this file with:
  2. .\" nroff -man [file]
  3. .\" $Id: curl_formadd.3,v 1.8 2004/02/27 15:34:06 bagder Exp $
  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 curl_httppost ** " firstitem,
  12. .BI "struct curl_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. After the \fIlastitem\fP pointer follow the real arguments.
  22. The pointers \fI*firstitem\fP and \fI*lastitem\fP should both be pointing to
  23. NULL in the first call to this function. All list-data will be allocated by
  24. the function itself. You must call \fIcurl_formfree\fP after the form post has
  25. been done to free the resources again.
  26. First, there are some basics you need to understand about multipart/formdata
  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
  29. FILENAME. Below here, we'll discuss on what options you use to set these
  30. properties in the parts you want to add to your post.
  31. .SH OPTIONS
  32. .B CURLFORM_COPYNAME
  33. followed by string is used to set the name of this part. libcurl copies the
  34. given data, so your application doesn't need to keep it around after this
  35. function call. If the name isn't zero terminated properly, or if you'd like it
  36. to contain zero bytes, you need to set the length of the name with
  37. \fBCURLFORM_NAMELENGTH\fP.
  38. .B CURLFORM_PTRNAME
  39. followed by a string is used for the name of this part. libcurl will use the
  40. pointer and refer to the data in your application, you must make sure it
  41. remains until curl no longer needs it. If the name isn't zero terminated
  42. properly, or if you'd like it to contain zero bytes, you need to set the
  43. length of the name with \fBCURLFORM_NAMELENGTH\fP.
  44. .B CURLFORM_COPYCONTENTS
  45. followed by a string is used for the contents of this part, the actual data to
  46. send away. libcurl copies the given data, so your application doesn't need to
  47. keep it around after this function call. If the data isn't zero terminated
  48. properly, or if you'd like it to contain zero bytes, you need to set the
  49. length of the name with \fBCURLFORM_CONTENTSLENGTH\fP.
  50. .B CURLFORM_PTRCONTENTS
  51. followed by a string is used for the contents of this part, the actual data to
  52. send away. libcurl will use the pointer and refer to the data in your
  53. application, you must make sure it remains until curl no longer needs it. If
  54. the data isn't zero terminated properly, or if you'd like it to contain zero
  55. bytes, you need to set the length of the name with
  56. \fBCURLFORM_CONTENTSLENGTH\fP.
  57. .B CURLFORM_FILECONTENT
  58. followed by a file name, makes that file read and the contents will be used in
  59. as data in this part.
  60. .B CURLFORM_FILE
  61. followed by a file name, makes this part a file upload part. It sets the file
  62. name field to the actual file name used here, it gets the contents of the file
  63. and passes as data and sets the content-type if the given file match one of
  64. the new internally known file extension. For \fBCURLFORM_FILE\fP the user may
  65. send one or more files in one part by providing multiple \fBCURLFORM_FILE\fP
  66. arguments each followed by the filename (and each CURLFORM_FILE is allowed to
  67. have a CURLFORM_CONTENTTYPE).
  68. .B CURLFORM_CONTENTTYPE
  69. followed by a pointer to a string with a content-type will make curl use this
  70. given content-type for this file upload part, possibly instead of an
  71. internally chosen one.
  72. .B CURLFORM_FILENAME
  73. followed by a pointer to a string to a name, will make libcurl use the given
  74. name in the file upload part, intead of the actual file name given to
  75. \fICURLFORM_FILE\fP.
  76. .B BCURLFORM_BUFFER
  77. followed by a string, tells libcurl that a buffer is to be used to upload data
  78. instead of using a file. The given string is used as the value of the file
  79. name field in the content header.
  80. .B CURLFORM_BUFFERPTR
  81. followed by a pointer to a data area, tells libcurl the address of the buffer
  82. containing data to upload (as indicated with \fICURLFORM_BUFFER\fP). The
  83. buffer containing this data must not be freed until after
  84. \fIcurl_easy_cleanup(3)\fP is called.
  85. .B CURLFORM_BUFFERLENGTH
  86. followed by a long with the size of the \fICURLFORM_BUFFERPTR\fP data area,
  87. tells libcurl the length of the buffer to upload.
  88. .B CURLFORM_ARRAY
  89. Another possibility to send options to curl_formadd() is the
  90. \fBCURLFORM_ARRAY\fP option, that passes a struct curl_forms array pointer as
  91. its value. Each curl_forms structure element has a CURLformoption and a char
  92. pointer. The final element in the array must be a CURLFORM_END. All available
  93. options can be used in an array, except the CURLFORM_ARRAY option itself! The
  94. last argument in such an array must always be \fBCURLFORM_END\fP.
  95. .B CURLFORM_CONTENTHEADER
  96. specifies extra headers for the form POST section. This takes a curl_slist
  97. prepared in the usual way using \fBcurl_slist_append\fP and appends the list
  98. of headers to those libcurl automatically generates. The list must exist while
  99. the POST occurs, if you free it before the post completes you may experience
  100. problems.
  101. When you've passed the HttpPost pointer to \fIcurl_easy_setopt(3)\fP (using
  102. the \fICURLOPT_HTTPPOST\fP option), you must not free the list until after
  103. you've called \fIcurl_easy_cleanup(3)\fP for the curl handle.
  104. See example below.
  105. .SH RETURN VALUE
  106. 0 means everything was ok, non-zero means an error occurred as
  107. .I <curl/curl.h>
  108. defines.
  109. .SH EXAMPLE
  110. .nf
  111. struct HttpPost* post = NULL;
  112. struct HttpPost* last = NULL;
  113. char namebuffer[] = "name buffer";
  114. long namelength = strlen(namebuffer);
  115. char buffer[] = "test buffer";
  116. char htmlbuffer[] = "<HTML>test buffer</HTML>";
  117. long htmlbufferlength = strlen(htmlbuffer);
  118. struct curl_forms forms[3];
  119. char file1[] = "my-face.jpg";
  120. char file2[] = "your-face.jpg";
  121. /* add null character into htmlbuffer, to demonstrate that
  122. transfers of buffers containing null characters actually work
  123. */
  124. htmlbuffer[8] = '\\0';
  125. /* Add simple name/content section */
  126. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
  127. CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
  128. /* Add simple name/content/contenttype section */
  129. curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
  130. CURLFORM_COPYCONTENTS, "<HTML></HTML>",
  131. CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
  132. /* Add name/ptrcontent section */
  133. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
  134. CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
  135. /* Add ptrname/ptrcontent section */
  136. curl_formadd(&post, &last, CURLFORM_PTRNAME, namebuffer,
  137. CURLFORM_PTRCONTENTS, buffer, CURLFORM_NAMELENGTH,
  138. namelength, CURLFORM_END);
  139. /* Add name/ptrcontent/contenttype section */
  140. curl_formadd(&post, &last, CURLFORM_COPYNAME, "html_code_with_hole",
  141. CURLFORM_PTRCONTENTS, htmlbuffer,
  142. CURLFORM_CONTENTSLENGTH, htmlbufferlength,
  143. CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
  144. /* Add simple file section */
  145. curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
  146. CURLFORM_FILE, "my-face.jpg", CURLFORM_END);
  147. /* Add file/contenttype section */
  148. curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture",
  149. CURLFORM_FILE, "my-face.jpg",
  150. CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END);
  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. /* Add two file section using CURLFORM_ARRAY */
  156. forms[0].option = CURLFORM_FILE;
  157. forms[0].value = file1;
  158. forms[1].option = CURLFORM_FILE;
  159. forms[1].value = file2;
  160. forms[2].option = CURLFORM_END;
  161. /* Add a buffer to upload */
  162. curl_formadd(&post, &last,
  163. CURLFORM_COPYNAME, "name",
  164. CURLFORM_BUFFER, "data",
  165. CURLFORM_BUFFERPTR, record,
  166. CURLFORM_BUFFERLENGTH, record_length,
  167. CURLFORM_END);
  168. /* no option needed for the end marker */
  169. curl_formadd(&post, &last, CURLFORM_COPYNAME, "pictures",
  170. CURLFORM_ARRAY, forms, CURLFORM_END);
  171. /* Add the content of a file as a normal post text value */
  172. curl_formadd(&post, &last, CURLFORM_COPYNAME, "filecontent",
  173. CURLFORM_FILECONTENT, ".bashrc", CURLFORM_END);
  174. /* Set the form info */
  175. curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
  176. .SH "SEE ALSO"
  177. .BR curl_easy_setopt "(3), "
  178. .BR curl_formparse "(3) [deprecated], "
  179. .BR curl_formfree "(3)"