CURLOPT_POSTFIELDS.3 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. .\" generated by cd2nroff 0.1 from CURLOPT_POSTFIELDS.md
  2. .TH CURLOPT_POSTFIELDS 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_POSTFIELDS \- data to POST to server
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_POSTFIELDS, char *postdata);
  9. .fi
  10. .SH DESCRIPTION
  11. Pass a char pointer as parameter, pointing to the data buffer to use in an
  12. HTTP POST operation or an MQTT subscribe. The data must be formatted and
  13. encoded the way you want the server to receive it. libcurl does not convert or
  14. encode it in any way. For example, a web server may assume that this data is
  15. URL encoded.
  16. The data pointed to is NOT copied by the library: as a consequence, it must be
  17. preserved by the calling application until the associated transfer finishes.
  18. This behavior can be changed (so libcurl does copy the data) by instead using
  19. the \fICURLOPT_COPYPOSTFIELDS(3)\fP option.
  20. This POST is a normal \fBapplication/x\-www\-form\-urlencoded\fP kind (and
  21. libcurl sets that Content\-Type by default when this option is used), which is
  22. commonly used by HTML forms. Change Content\-Type with
  23. \fICURLOPT_HTTPHEADER(3)\fP.
  24. You can use \fIcurl_easy_escape(3)\fP to URL encode your data, if
  25. necessary. It returns a pointer to an encoded string that can be passed as
  26. \fIpostdata\fP.
  27. Using \fICURLOPT_POSTFIELDS(3)\fP implies setting \fICURLOPT_POST(3)\fP to 1.
  28. If \fICURLOPT_POSTFIELDS(3)\fP is explicitly set to NULL then libcurl gets the POST
  29. data from the read callback. To send a zero\-length (empty) POST, set
  30. \fICURLOPT_POSTFIELDS(3)\fP to an empty string, or set \fICURLOPT_POST(3)\fP to 1 and
  31. \fICURLOPT_POSTFIELDSIZE(3)\fP to 0.
  32. libcurl assumes this option points to a null\-terminated string unless you also
  33. set \fICURLOPT_POSTFIELDSIZE(3)\fP to specify the length of the provided data,
  34. which then is strictly required if you want to send off null bytes included in
  35. the data.
  36. Using POST with HTTP 1.1 implies the use of a "Expect: 100\-continue" header,
  37. and libcurl adds that header automatically if the POST is either known to be
  38. larger than 1MB or if the expected size is unknown. You can disable this
  39. header with \fICURLOPT_HTTPHEADER(3)\fP as usual.
  40. To make \fBmultipart/formdata\fP posts, check out the
  41. \fICURLOPT_MIMEPOST(3)\fP option combined with \fIcurl_mime_init(3)\fP.
  42. .SH DEFAULT
  43. NULL
  44. .SH PROTOCOLS
  45. This functionality affects http and mqtt
  46. .SH EXAMPLE
  47. .nf
  48. /* send an application/x-www-form-urlencoded POST */
  49. int main(void)
  50. {
  51. CURL *curl = curl_easy_init();
  52. if(curl) {
  53. const char *data = "data to send";
  54. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  55. /* size of the POST data if strlen() is not good enough */
  56. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 12L);
  57. /* pass in a pointer to the data - libcurl does not copy */
  58. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  59. curl_easy_perform(curl);
  60. }
  61. /* send an application/json POST */
  62. curl = curl_easy_init();
  63. if(curl) {
  64. const char *json = "{\\"name\\": \\"daniel\\"}";
  65. struct curl_slist *slist1 = NULL;
  66. slist1 = curl_slist_append(slist1, "Content-Type: application/json");
  67. slist1 = curl_slist_append(slist1, "Accept: application/json");
  68. /* set custom headers */
  69. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
  70. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  71. /* pass in a pointer to the data - libcurl does not copy */
  72. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
  73. curl_easy_perform(curl);
  74. }
  75. }
  76. .fi
  77. .SH AVAILABILITY
  78. Added in curl 7.1
  79. .SH RETURN VALUE
  80. Returns CURLE_OK
  81. .SH SEE ALSO
  82. .BR CURLOPT_COPYPOSTFIELDS (3),
  83. .BR CURLOPT_MIMEPOST (3),
  84. .BR CURLOPT_POSTFIELDSIZE (3),
  85. .BR CURLOPT_READFUNCTION (3),
  86. .BR CURLOPT_UPLOAD (3)