CURLOPT_POST.md (3105B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_POST 5 Section: 3 6 Source: libcurl 7 Protocol: 8 - HTTP 9 See-also: 10 - CURLOPT_HTTPPOST (3) 11 - CURLOPT_POSTFIELDS (3) 12 - CURLOPT_UPLOAD (3) 13 Added-in: 7.1 14 --- 15 16 # NAME 17 18 CURLOPT_POST - make an HTTP POST 19 20 # SYNOPSIS 21 22 ~~~c 23 #include <curl/curl.h> 24 25 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_POST, long post); 26 ~~~ 27 28 # DESCRIPTION 29 30 A parameter set to 1 tells libcurl to do a regular HTTP post. This also makes 31 libcurl use a "Content-Type: application/x-www-form-urlencoded" header. This 32 is the most commonly used POST method. 33 34 Use one of CURLOPT_POSTFIELDS(3) or CURLOPT_COPYPOSTFIELDS(3) 35 options to specify what data to post and CURLOPT_POSTFIELDSIZE(3) or 36 CURLOPT_POSTFIELDSIZE_LARGE(3) to set the data size. 37 38 Optionally, you can provide data to POST using the 39 CURLOPT_READFUNCTION(3) and CURLOPT_READDATA(3) options but then 40 you must make sure to not set CURLOPT_POSTFIELDS(3) to anything but 41 NULL. When providing data with a callback, you must transmit it using chunked 42 transfer-encoding or you must set the size of the data with the 43 CURLOPT_POSTFIELDSIZE(3) or CURLOPT_POSTFIELDSIZE_LARGE(3) 44 options. To enable chunked encoding, you simply pass in the appropriate 45 Transfer-Encoding header, see the post-callback.c example. 46 47 You can override the default POST Content-Type: header by setting your own 48 with CURLOPT_HTTPHEADER(3). 49 50 Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue" header. 51 You can disable this header with CURLOPT_HTTPHEADER(3) as usual. 52 53 If you use POST to an HTTP 1.1 server, you can send data without knowing the 54 size before starting the POST if you use chunked encoding. You enable this by 55 adding a header like "Transfer-Encoding: chunked" with 56 CURLOPT_HTTPHEADER(3). With HTTP 1.0 or without chunked transfer, you 57 must specify the size in the request. (Since 7.66.0, libcurl automatically 58 uses chunked encoding for POSTs if the size is unknown.) 59 60 When setting CURLOPT_POST(3) to 1, libcurl automatically sets 61 CURLOPT_NOBODY(3) and CURLOPT_HTTPGET(3) to 0. 62 63 If you issue a POST request and then want to make a HEAD or GET using the same 64 reused handle, you must explicitly set the new request type using 65 CURLOPT_NOBODY(3) or CURLOPT_HTTPGET(3) or similar. 66 67 When setting CURLOPT_POST(3) to 0, libcurl resets the request type to the 68 default to disable the POST. Typically that means gets reset to GET. Instead 69 you should set a new request type explicitly as described above. 70 71 # DEFAULT 72 73 0, disabled 74 75 # %PROTOCOLS% 76 77 # EXAMPLE 78 79 ~~~c 80 int main(void) 81 { 82 CURL *curl = curl_easy_init(); 83 if(curl) { 84 CURLcode res; 85 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/foo.bin"); 86 curl_easy_setopt(curl, CURLOPT_POST, 1L); 87 88 /* set up the read callback with CURLOPT_READFUNCTION */ 89 90 res = curl_easy_perform(curl); 91 92 curl_easy_cleanup(curl); 93 } 94 } 95 ~~~ 96 97 # %AVAILABILITY% 98 99 # RETURN VALUE 100 101 curl_easy_setopt(3) returns a CURLcode indicating success or error. 102 103 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 104 libcurl-errors(3).