1CURLOPT_WRITEFUNCTION(3) curl_easy_setopt options CURLOPT_WRITEFUNCTION(3)
2
3
4
6 CURLOPT_WRITEFUNCTION - set callback for writing received data
7
9 #include <curl/curl.h>
10
11 size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);
12
13 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEFUNCTION, write_callback);
14
16 Pass a pointer to your callback function, which should match the proto‐
17 type shown above.
18
19 This callback function gets called by libcurl as soon as there is data
20 received that needs to be saved. For most transfers, this callback gets
21 called many times and each invoke delivers another chunk of data. ptr
22 points to the delivered data, and the size of that data is nmemb; size
23 is always 1.
24
25 The callback function will be passed as much data as possible in all
26 invokes, but you must not make any assumptions. It may be one byte, it
27 may be thousands. The maximum amount of body data that will be passed
28 to the write callback is defined in the curl.h header file:
29 CURL_MAX_WRITE_SIZE (the usual default is 16K). If CURLOPT_HEADER(3) is
30 enabled, which makes header data get passed to the write callback, you
31 can get up to CURL_MAX_HTTP_HEADER bytes of header data passed into it.
32 This usually means 100K.
33
34 This function may be called with zero bytes data if the transferred
35 file is empty.
36
37 The data passed to this function will not be zero terminated!
38
39 Set the userdata argument with the CURLOPT_WRITEDATA(3) option.
40
41 Your callback should return the number of bytes actually taken care of.
42 If that amount differs from the amount passed to your callback func‐
43 tion, it'll signal an error condition to the library. This will cause
44 the transfer to get aborted and the libcurl function used will return
45 CURLE_WRITE_ERROR.
46
47 If your callback function returns CURL_WRITEFUNC_PAUSE it will cause
48 this transfer to become paused. See curl_easy_pause(3) for further
49 details.
50
51 Set this option to NULL to get the internal default function used
52 instead of your callback. The internal default function will write the
53 data to the FILE * given with CURLOPT_WRITEDATA(3).
54
56 libcurl will use 'fwrite' as a callback by default.
57
59 For all protocols
60
62 Support for the CURL_WRITEFUNC_PAUSE return code was added in version
63 7.18.0.
64
66 This will return CURLE_OK.
67
69 A common technique is to use this callback to store the incoming data
70 into a dynamically growing allocated buffer. Like in the getinmemory
71 example: https://curl.haxx.se/libcurl/c/getinmemory.html
72
74 CURLOPT_WRITEDATA(3), CURLOPT_READFUNCTION(3),
75
76
77
78libcurl 7.69.1 November 23, 2018 CURLOPT_WRITEFUNCTION(3)