1CURLOPT_READFUNCTION(3) curl_easy_setopt options CURLOPT_READFUNCTION(3)
2
3
4
6 CURLOPT_READFUNCTION - read callback for data uploads
7
9 #include <curl/curl.h>
10
11 size_t read_callback(char *buffer, size_t size, size_t nitems, void
12 *userdata);
13
14 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_READFUNCTION,
15 read_callback);
16
17
19 Pass a pointer to your callback function, as the prototype shows above.
20
21 This callback function gets called by libcurl as soon as it needs to
22 read data in order to send it to the peer - like if you ask it to up‐
23 load or post data to the server. The data area pointed at by the
24 pointer buffer should be filled up with at most size multiplied with
25 nitems number of bytes by your function.
26
27 Set the userdata argument with the CURLOPT_READDATA(3) option.
28
29 Your function must return the actual number of bytes that it stored in
30 the data area pointed at by the pointer buffer. Returning 0 will signal
31 end-of-file to the library and cause it to stop the current transfer.
32
33 If you stop the current transfer by returning 0 "pre-maturely" (i.e be‐
34 fore the server expected it, like when you've said you will upload N
35 bytes and you upload less than N bytes), you may experience that the
36 server "hangs" waiting for the rest of the data that won't come.
37
38 The read callback may return CURL_READFUNC_ABORT to stop the current
39 operation immediately, resulting in a CURLE_ABORTED_BY_CALLBACK error
40 code from the transfer.
41
42 The callback can return CURL_READFUNC_PAUSE to cause reading from this
43 connection to pause. See curl_easy_pause(3) for further details.
44
45 Bugs: when doing TFTP uploads, you must return the exact amount of data
46 that the callback wants, or it will be considered the final packet by
47 the server end and the transfer will end there.
48
49 If you set this callback pointer to NULL, or don't set it at all, the
50 default internal read function will be used. It is doing an fread() on
51 the FILE * userdata set with CURLOPT_READDATA(3).
52
53 You can set the total size of the data you are sending by using CUR‐
54 LOPT_INFILESIZE_LARGE(3) or CURLOPT_POSTFIELDSIZE_LARGE(3), depending
55 on the type of transfer. For some transfer types it may be required and
56 it allows for better error checking.
57
59 The default internal read callback is fread().
60
62 This is used for all protocols when doing uploads.
63
65 size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
66 {
67 FILE *readhere = (FILE *)userdata;
68 curl_off_t nread;
69
70 /* copy as much data as possible into the 'ptr' buffer, but no more than
71 'size' * 'nmemb' bytes! */
72 size_t retcode = fread(ptr, size, nmemb, readhere);
73
74 nread = (curl_off_t)retcode;
75
76 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
77 " bytes from file\n", nread);
78 return retcode;
79 }
80
81 void setup(char *uploadthis)
82 {
83 FILE *file = fopen(uploadthis, "rb");
84 CURLcode result;
85
86 /* set callback to use */
87 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
88
89 /* pass in suitable argument to callback */
90 curl_easy_setopt(curl, CURLOPT_READDATA, (void *)file);
91
92 result = curl_easy_perform(curl);
93 }
94
96 CURL_READFUNC_PAUSE return code was added in 7.18.0 and CURL_READ‐
97 FUNC_ABORT was added in 7.12.1.
98
100 This will return CURLE_OK.
101
103 CURLOPT_READDATA(3), CURLOPT_WRITEFUNCTION(3), CURLOPT_SEEKFUNCTION(3),
104 CURLOPT_UPLOAD(3), CURLOPT_POST(3), CURLOPT_UPLOAD_BUFFERSIZE(3),
105
106
107
108libcurl 7.76.1 December 30, 2020 CURLOPT_READFUNCTION(3)