xref: /aosp_15_r20/external/curl/docs/libcurl/opts/CURLOPT_POST.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: CURLOPT_POST
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerProtocol:
8*6236dae4SAndroid Build Coastguard Worker  - HTTP
9*6236dae4SAndroid Build Coastguard WorkerSee-also:
10*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_HTTPPOST (3)
11*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_POSTFIELDS (3)
12*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_UPLOAD (3)
13*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.1
14*6236dae4SAndroid Build Coastguard Worker---
15*6236dae4SAndroid Build Coastguard Worker
16*6236dae4SAndroid Build Coastguard Worker# NAME
17*6236dae4SAndroid Build Coastguard Worker
18*6236dae4SAndroid Build Coastguard WorkerCURLOPT_POST - make an HTTP POST
19*6236dae4SAndroid Build Coastguard Worker
20*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS
21*6236dae4SAndroid Build Coastguard Worker
22*6236dae4SAndroid Build Coastguard Worker~~~c
23*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h>
24*6236dae4SAndroid Build Coastguard Worker
25*6236dae4SAndroid Build Coastguard WorkerCURLcode curl_easy_setopt(CURL *handle, CURLOPT_POST, long post);
26*6236dae4SAndroid Build Coastguard Worker~~~
27*6236dae4SAndroid Build Coastguard Worker
28*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
29*6236dae4SAndroid Build Coastguard Worker
30*6236dae4SAndroid Build Coastguard WorkerA parameter set to 1 tells libcurl to do a regular HTTP post. This also makes
31*6236dae4SAndroid Build Coastguard Workerlibcurl use a "Content-Type: application/x-www-form-urlencoded" header. This
32*6236dae4SAndroid Build Coastguard Workeris the most commonly used POST method.
33*6236dae4SAndroid Build Coastguard Worker
34*6236dae4SAndroid Build Coastguard WorkerUse one of CURLOPT_POSTFIELDS(3) or CURLOPT_COPYPOSTFIELDS(3)
35*6236dae4SAndroid Build Coastguard Workeroptions to specify what data to post and CURLOPT_POSTFIELDSIZE(3) or
36*6236dae4SAndroid Build Coastguard WorkerCURLOPT_POSTFIELDSIZE_LARGE(3) to set the data size.
37*6236dae4SAndroid Build Coastguard Worker
38*6236dae4SAndroid Build Coastguard WorkerOptionally, you can provide data to POST using the
39*6236dae4SAndroid Build Coastguard WorkerCURLOPT_READFUNCTION(3) and CURLOPT_READDATA(3) options but then
40*6236dae4SAndroid Build Coastguard Workeryou must make sure to not set CURLOPT_POSTFIELDS(3) to anything but
41*6236dae4SAndroid Build Coastguard WorkerNULL. When providing data with a callback, you must transmit it using chunked
42*6236dae4SAndroid Build Coastguard Workertransfer-encoding or you must set the size of the data with the
43*6236dae4SAndroid Build Coastguard WorkerCURLOPT_POSTFIELDSIZE(3) or CURLOPT_POSTFIELDSIZE_LARGE(3)
44*6236dae4SAndroid Build Coastguard Workeroptions. To enable chunked encoding, you simply pass in the appropriate
45*6236dae4SAndroid Build Coastguard WorkerTransfer-Encoding header, see the post-callback.c example.
46*6236dae4SAndroid Build Coastguard Worker
47*6236dae4SAndroid Build Coastguard WorkerYou can override the default POST Content-Type: header by setting your own
48*6236dae4SAndroid Build Coastguard Workerwith CURLOPT_HTTPHEADER(3).
49*6236dae4SAndroid Build Coastguard Worker
50*6236dae4SAndroid Build Coastguard WorkerUsing POST with HTTP 1.1 implies the use of a "Expect: 100-continue" header.
51*6236dae4SAndroid Build Coastguard WorkerYou can disable this header with CURLOPT_HTTPHEADER(3) as usual.
52*6236dae4SAndroid Build Coastguard Worker
53*6236dae4SAndroid Build Coastguard WorkerIf you use POST to an HTTP 1.1 server, you can send data without knowing the
54*6236dae4SAndroid Build Coastguard Workersize before starting the POST if you use chunked encoding. You enable this by
55*6236dae4SAndroid Build Coastguard Workeradding a header like "Transfer-Encoding: chunked" with
56*6236dae4SAndroid Build Coastguard WorkerCURLOPT_HTTPHEADER(3). With HTTP 1.0 or without chunked transfer, you
57*6236dae4SAndroid Build Coastguard Workermust specify the size in the request. (Since 7.66.0, libcurl automatically
58*6236dae4SAndroid Build Coastguard Workeruses chunked encoding for POSTs if the size is unknown.)
59*6236dae4SAndroid Build Coastguard Worker
60*6236dae4SAndroid Build Coastguard WorkerWhen setting CURLOPT_POST(3) to 1, libcurl automatically sets
61*6236dae4SAndroid Build Coastguard WorkerCURLOPT_NOBODY(3) and CURLOPT_HTTPGET(3) to 0.
62*6236dae4SAndroid Build Coastguard Worker
63*6236dae4SAndroid Build Coastguard WorkerIf you issue a POST request and then want to make a HEAD or GET using the same
64*6236dae4SAndroid Build Coastguard Workerreused handle, you must explicitly set the new request type using
65*6236dae4SAndroid Build Coastguard WorkerCURLOPT_NOBODY(3) or CURLOPT_HTTPGET(3) or similar.
66*6236dae4SAndroid Build Coastguard Worker
67*6236dae4SAndroid Build Coastguard WorkerWhen setting CURLOPT_POST(3) to 0, libcurl resets the request type to the
68*6236dae4SAndroid Build Coastguard Workerdefault to disable the POST. Typically that means gets reset to GET. Instead
69*6236dae4SAndroid Build Coastguard Workeryou should set a new request type explicitly as described above.
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard Worker# DEFAULT
72*6236dae4SAndroid Build Coastguard Worker
73*6236dae4SAndroid Build Coastguard Worker0, disabled
74*6236dae4SAndroid Build Coastguard Worker
75*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS%
76*6236dae4SAndroid Build Coastguard Worker
77*6236dae4SAndroid Build Coastguard Worker# EXAMPLE
78*6236dae4SAndroid Build Coastguard Worker
79*6236dae4SAndroid Build Coastguard Worker~~~c
80*6236dae4SAndroid Build Coastguard Workerint main(void)
81*6236dae4SAndroid Build Coastguard Worker{
82*6236dae4SAndroid Build Coastguard Worker  CURL *curl = curl_easy_init();
83*6236dae4SAndroid Build Coastguard Worker  if(curl) {
84*6236dae4SAndroid Build Coastguard Worker    CURLcode res;
85*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/foo.bin");
86*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_POST, 1L);
87*6236dae4SAndroid Build Coastguard Worker
88*6236dae4SAndroid Build Coastguard Worker    /* set up the read callback with CURLOPT_READFUNCTION */
89*6236dae4SAndroid Build Coastguard Worker
90*6236dae4SAndroid Build Coastguard Worker    res = curl_easy_perform(curl);
91*6236dae4SAndroid Build Coastguard Worker
92*6236dae4SAndroid Build Coastguard Worker    curl_easy_cleanup(curl);
93*6236dae4SAndroid Build Coastguard Worker  }
94*6236dae4SAndroid Build Coastguard Worker}
95*6236dae4SAndroid Build Coastguard Worker~~~
96*6236dae4SAndroid Build Coastguard Worker
97*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY%
98*6236dae4SAndroid Build Coastguard Worker
99*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE
100*6236dae4SAndroid Build Coastguard Worker
101*6236dae4SAndroid Build Coastguard WorkerReturns CURLE_OK if HTTP is supported, and CURLE_UNKNOWN_OPTION if not.
102