2
0
mirror of https://codeberg.org/kiss-community/repo synced 2024-11-19 21:20:07 -07:00
repo/core/curl/files/CURLOPT_POSTFIELDS.3
2024-03-30 13:28:04 -05:00

105 lines
3.6 KiB
Groff

.\" generated by cd2nroff 0.1 from CURLOPT_POSTFIELDS.md
.TH CURLOPT_POSTFIELDS 3 libcurl
.SH NAME
CURLOPT_POSTFIELDS \- data to POST to server
.SH SYNOPSIS
.nf
#include <curl/curl.h>
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_POSTFIELDS, char *postdata);
.fi
.SH DESCRIPTION
Pass a char pointer as parameter, pointing to the data buffer to use in an
HTTP POST operation or an MQTT subscribe. The data must be formatted and
encoded the way you want the server to receive it. libcurl does not convert or
encode it in any way. For example, a web server may assume that this data is
URL encoded.
The data pointed to is NOT copied by the library: as a consequence, it must be
preserved by the calling application until the associated transfer finishes.
This behavior can be changed (so libcurl does copy the data) by instead using
the \fICURLOPT_COPYPOSTFIELDS(3)\fP option.
This POST is a normal \fBapplication/x\-www\-form\-urlencoded\fP kind (and
libcurl sets that Content\-Type by default when this option is used), which is
commonly used by HTML forms. Change Content\-Type with
\fICURLOPT_HTTPHEADER(3)\fP.
You can use \fIcurl_easy_escape(3)\fP to URL encode your data, if
necessary. It returns a pointer to an encoded string that can be passed as
\fIpostdata\fP.
Using \fICURLOPT_POSTFIELDS(3)\fP implies setting \fICURLOPT_POST(3)\fP to 1.
If \fICURLOPT_POSTFIELDS(3)\fP is explicitly set to NULL then libcurl gets the POST
data from the read callback. To send a zero\-length (empty) POST, set
\fICURLOPT_POSTFIELDS(3)\fP to an empty string, or set \fICURLOPT_POST(3)\fP to 1 and
\fICURLOPT_POSTFIELDSIZE(3)\fP to 0.
libcurl assumes this option points to a null\-terminated string unless you also
set \fICURLOPT_POSTFIELDSIZE(3)\fP to specify the length of the provided data,
which then is strictly required if you want to send off null bytes included in
the data.
Using POST with HTTP 1.1 implies the use of a "Expect: 100\-continue" header,
and libcurl adds that header automatically if the POST is either known to be
larger than 1MB or if the expected size is unknown. You can disable this
header with \fICURLOPT_HTTPHEADER(3)\fP as usual.
To make \fBmultipart/formdata\fP posts, check out the
\fICURLOPT_MIMEPOST(3)\fP option combined with \fIcurl_mime_init(3)\fP.
.SH DEFAULT
NULL
.SH PROTOCOLS
HTTP and MQTT
.SH EXAMPLE
.nf
/* send an application/x-www-form-urlencoded POST */
int main(void)
{
CURL *curl = curl_easy_init();
if(curl) {
const char *data = "data to send";
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
/* size of the POST data if strlen() is not good enough */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 12L);
/* pass in a pointer to the data - libcurl does not copy */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_perform(curl);
}
/* send an application/json POST */
curl = curl_easy_init();
if(curl) {
const char *json = "{\\"name\\": \\"daniel\\"}";
struct curl_slist *slist1 = NULL;
slist1 = curl_slist_append(slist1, "Content-Type: application/json");
slist1 = curl_slist_append(slist1, "Accept: application/json");
/* set custom headers */
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
/* pass in a pointer to the data - libcurl does not copy */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
curl_easy_perform(curl);
}
}
.fi
.SH AVAILABILITY
Always
.SH RETURN VALUE
Returns CURLE_OK
.SH SEE ALSO
.BR CURLOPT_COPYPOSTFIELDS (3),
.BR CURLOPT_MIMEPOST (3),
.BR CURLOPT_POSTFIELDSIZE (3),
.BR CURLOPT_READFUNCTION (3),
.BR CURLOPT_UPLOAD (3)