1CURLOPT_READFUNCTION(3)    curl_easy_setopt options    CURLOPT_READFUNCTION(3)
2
3
4

NAME

6       CURLOPT_READFUNCTION - read callback for data uploads
7

SYNOPSIS

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

DESCRIPTION

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
23       upload  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
34       before 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

DEFAULT

54       The default internal read callback is fread().
55

PROTOCOLS

57       This is used for all protocols when doing uploads.
58

EXAMPLE

60       size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userdata)
61       {
62         FILE *readhere = (FILE *)userdata;
63         curl_off_t nread;
64
65         /* copy as much data as possible into the 'ptr' buffer, but no more than
66            'size' * 'nmemb' bytes! */
67         size_t retcode = fread(ptr, size, nmemb, readhere);
68
69         nread = (curl_off_t)retcode;
70
71         fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
72                 " bytes from file\n", nread);
73         return retcode;
74       }
75
76       void setup(char *uploadthis)
77       {
78         FILE *file = fopen(uploadthis, "rb");
79         CURLcode result;
80
81         /* set callback to use */
82         curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
83
84         /* pass in suitable argument to callback */
85         curl_easy_setopt(curl, CURLOPT_READDATA, (void *)file);
86
87         result = curl_easy_perform(curl);
88       }
89

AVAILABILITY

91       CURL_READFUNC_PAUSE  return  code  was  added  in 7.18.0 and CURL_READ‐
92       FUNC_ABORT was added in 7.12.1.
93

RETURN VALUE

95       This will return CURLE_OK.
96

SEE ALSO

98       CURLOPT_READDATA(3), CURLOPT_WRITEFUNCTION(3), CURLOPT_SEEKFUNCTION(3),
99       CURLOPT_UPLOAD(3), CURLOPT_POST(3), CURLOPT_UPLOAD_BUFFERSIZE(3),
100
101
102
103libcurl 7.69.1                 January 06, 2020        CURLOPT_READFUNCTION(3)
Impressum