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 null-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 struct memory {
70 char *response;
71 size_t size;
72 };
73
74 static size_t cb(void *data, size_t size, size_t nmemb, void *userp)
75 {
76 size_t realsize = size * nmemb;
77 struct memory *mem = (struct memory *)userp;
78
79 char *ptr = realloc(mem->response, mem->size + realsize + 1);
80 if(ptr == NULL)
81 return 0; /* out of memory! */
82
83 mem->response = ptr;
84 memcpy(&(mem->response[mem->size]), data, realsize);
85 mem->size += realsize;
86 mem->response[mem->size] = 0;
87
88 return realsize;
89 }
90
91 struct memory chunk;
92
93 /* send all data to this function */
94 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, cb);
95
96 /* we pass our 'chunk' struct to the callback function */
97 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
98
100 CURLOPT_WRITEDATA(3), CURLOPT_READFUNCTION(3), CURLOPT_HEADERFUNC‐
101 TION(3),
102
103
104
105libcurl 7.71.1 June 25, 2020 CURLOPT_WRITEFUNCTION(3)