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 de‐
49 tails.
50
51 Set this option to NULL to get the internal default function used in‐
52 stead of your callback. The internal default function will write the
53 data to the FILE * given with CURLOPT_WRITEDATA(3).
54
55 This option doesn't enable HSTS, you need to use CURLOPT_HSTS_CTRL(3)
56 to do that.
57
59 libcurl will use 'fwrite' as a callback by default.
60
62 For all protocols
63
65 Support for the CURL_WRITEFUNC_PAUSE return code was added in version
66 7.18.0.
67
69 This will return CURLE_OK.
70
72 struct memory {
73 char *response;
74 size_t size;
75 };
76
77 static size_t cb(void *data, size_t size, size_t nmemb, void *userp)
78 {
79 size_t realsize = size * nmemb;
80 struct memory *mem = (struct memory *)userp;
81
82 char *ptr = realloc(mem->response, mem->size + realsize + 1);
83 if(ptr == NULL)
84 return 0; /* out of memory! */
85
86 mem->response = ptr;
87 memcpy(&(mem->response[mem->size]), data, realsize);
88 mem->size += realsize;
89 mem->response[mem->size] = 0;
90
91 return realsize;
92 }
93
94 struct memory chunk;
95
96 /* send all data to this function */
97 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, cb);
98
99 /* we pass our 'chunk' struct to the callback function */
100 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
101
103 CURLOPT_WRITEDATA(3), CURLOPT_READFUNCTION(3), CURLOPT_HEADERFUNC‐
104 TION(3),
105
106
107
108libcurl 7.76.1 November 04, 2020 CURLOPT_WRITEFUNCTION(3)