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 *userdata);
12
13       CURLcode curl_easy_setopt(CURL *handle, CURLOPT_READFUNCTION, read_callback);
14

DESCRIPTION

16       Pass a pointer to your callback function, as the prototype shows above.
17
18       This  callback  function  gets called by libcurl as soon as it needs to
19       read data in order to send it to the peer - like if you ask it  to  up‐
20       load  or  post  data  to  the  server.  The data area pointed at by the
21       pointer buffer should be filled up with at most  size  multiplied  with
22       nitems number of bytes by your function.
23
24       Set the userdata argument with the CURLOPT_READDATA(3) option.
25
26       Your  function must return the actual number of bytes that it stored in
27       the data area pointed at by the pointer buffer. Returning 0 will signal
28       end-of-file to the library and cause it to stop the current transfer.
29
30       If you stop the current transfer by returning 0 "pre-maturely" (i.e be‐
31       fore the server expected it, like when you have said you will upload  N
32       bytes  and  you  upload less than N bytes), you may experience that the
33       server "hangs" waiting for the rest of the data that will not come.
34
35       The read callback may return CURL_READFUNC_ABORT to  stop  the  current
36       operation  immediately,  resulting in a CURLE_ABORTED_BY_CALLBACK error
37       code from the transfer.
38
39       The callback can return CURL_READFUNC_PAUSE to cause reading from  this
40       connection to pause. See curl_easy_pause(3) for further details.
41
42       Bugs: when doing TFTP uploads, you must return the exact amount of data
43       that the callback wants, or it will be considered the final  packet  by
44       the server end and the transfer will end there.
45
46       If  you set this callback pointer to NULL, or do not set it at all, the
47       default internal read function will be used. It is doing an fread()  on
48       the FILE * userdata set with CURLOPT_READDATA(3).
49
50       You  can  set  the total size of the data you are sending by using CUR‐
51       LOPT_INFILESIZE_LARGE(3) or  CURLOPT_POSTFIELDSIZE_LARGE(3),  depending
52       on the type of transfer. For some transfer types it may be required and
53       it allows for better error checking.
54

DEFAULT

56       The default internal read callback is fread().
57

PROTOCOLS

59       This is used for all protocols when doing uploads.
60

EXAMPLE

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

AVAILABILITY

93       CURL_READFUNC_PAUSE return code was  added  in  7.18.0  and  CURL_READ‐
94       FUNC_ABORT was added in 7.12.1.
95

RETURN VALUE

97       This will return CURLE_OK.
98

SEE ALSO

100       CURLOPT_READDATA(3), CURLOPT_WRITEFUNCTION(3), CURLOPT_SEEKFUNCTION(3),
101       CURLOPT_UPLOAD(3), CURLOPT_POST(3), CURLOPT_UPLOAD_BUFFERSIZE(3),
102
103
104
105libcurl 7.85.0                   May 17, 2022          CURLOPT_READFUNCTION(3)
Impressum