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

NAME

6       CURLOPT_WRITEFUNCTION - callback for writing received data
7

SYNOPSIS

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

DESCRIPTION

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 will 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       You can also abort  the  transfer  by  returning  CURL_WRITEFUNC_ERROR.
48       (7.87.0)
49
50       If  your  callback  function returns CURL_WRITEFUNC_PAUSE it will cause
51       this transfer to become paused.  See curl_easy_pause(3) for further de‐
52       tails.
53
54       Set  this  option to NULL to get the internal default function used in‐
55       stead of your callback. The internal default function  will  write  the
56       data to the FILE * given with CURLOPT_WRITEDATA(3).
57
58       This  option does not enable HSTS, you need to use CURLOPT_HSTS_CTRL(3)
59       to do that.
60

DEFAULT

62       libcurl will use 'fwrite' as a callback by default.
63

PROTOCOLS

65       For all protocols
66

EXAMPLE

68       struct memory {
69         char *response;
70         size_t size;
71       };
72
73       static size_t cb(void *data, size_t size, size_t nmemb, void *clientp)
74       {
75         size_t realsize = size * nmemb;
76         struct memory *mem = (struct memory *)clientp;
77
78         char *ptr = realloc(mem->response, mem->size + realsize + 1);
79         if(ptr == NULL)
80           return 0;  /* out of memory! */
81
82         mem->response = ptr;
83         memcpy(&(mem->response[mem->size]), data, realsize);
84         mem->size += realsize;
85         mem->response[mem->size] = 0;
86
87         return realsize;
88       }
89
90       struct memory chunk = {0};
91       CURLcode res;
92       CURL *curl_handle = curl_easy_init();
93
94       if (curl_handle)
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
102         /* send a request */
103         res = curl_easy_perform(curl_handle);
104
105         /* remember to free the buffer */
106         free(chunk.response)
107
108         curl_easy_cleanup(curl_handle);
109       }
110

AVAILABILITY

112       Support for the CURL_WRITEFUNC_PAUSE return code was added  in  version
113       7.18.0.
114

RETURN VALUE

116       This will return CURLE_OK.
117

SEE ALSO

119       CURLOPT_WRITEDATA(3),    CURLOPT_READFUNCTION(3),   CURLOPT_HEADERFUNC‐
120       TION(3),
121
122
123
124libcurl 8.0.1                  February 07, 2023      CURLOPT_WRITEFUNCTION(3)
Impressum