CURLOPT_UPLOAD.md (2554B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_UPLOAD 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_INFILESIZE_LARGE (3) 9 - CURLOPT_PUT (3) 10 - CURLOPT_READFUNCTION (3) 11 Protocol: 12 - All 13 Added-in: 7.1 14 --- 15 16 # NAME 17 18 CURLOPT_UPLOAD - data upload 19 20 # SYNOPSIS 21 22 ~~~c 23 #include <curl/curl.h> 24 25 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_UPLOAD, long upload); 26 ~~~ 27 28 # DESCRIPTION 29 30 The long parameter *upload* set to 1 tells the library to prepare for and 31 perform an upload. The CURLOPT_READDATA(3) and CURLOPT_INFILESIZE(3) or 32 CURLOPT_INFILESIZE_LARGE(3) options are also interesting for uploads. If the 33 protocol is HTTP, uploading means using the PUT request unless you tell 34 libcurl otherwise. 35 36 Using PUT with HTTP 1.1 implies the use of a "Expect: 100-continue" header. 37 You can disable this header with CURLOPT_HTTPHEADER(3) as usual. 38 39 If you use PUT to an HTTP 1.1 server, you can upload data without knowing the 40 size before starting the transfer. The library enables this by adding a header 41 "Transfer-Encoding: chunked". With HTTP 1.0 or if you prefer not to use 42 chunked transfer, you must specify the size of the data with 43 CURLOPT_INFILESIZE(3) or CURLOPT_INFILESIZE_LARGE(3). 44 45 # DEFAULT 46 47 0 48 49 # %PROTOCOLS% 50 51 # EXAMPLE 52 53 ~~~c 54 static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *userdata) 55 { 56 FILE *src = userdata; 57 /* copy as much data as possible into the 'ptr' buffer, but no more than 58 'size' * 'nmemb' bytes */ 59 size_t retcode = fread(ptr, size, nmemb, src); 60 61 return retcode; 62 } 63 64 int main(void) 65 { 66 CURL *curl = curl_easy_init(); 67 if(curl) { 68 FILE *src = fopen("local-file", "r"); 69 curl_off_t fsize = 1234; /* set this to the size of the input file */ 70 71 /* we want to use our own read function */ 72 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb); 73 74 /* enable uploading */ 75 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 76 77 /* specify target */ 78 curl_easy_setopt(curl, CURLOPT_URL, "ftp://example.com/dir/to/newfile"); 79 80 /* now specify which pointer to pass to our callback */ 81 curl_easy_setopt(curl, CURLOPT_READDATA, src); 82 83 /* Set the size of the file to upload */ 84 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize); 85 86 /* Now run off and do what you have been told */ 87 curl_easy_perform(curl); 88 } 89 } 90 ~~~ 91 92 # %AVAILABILITY% 93 94 # RETURN VALUE 95 96 curl_easy_setopt(3) returns a CURLcode indicating success or error. 97 98 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 99 libcurl-errors(3).