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       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 does not enable HSTS, you need to use  CURLOPT_HSTS_CTRL(3)
56       to do that.
57

DEFAULT

59       libcurl will use 'fwrite' as a callback by default.
60

PROTOCOLS

62       For all protocols
63

EXAMPLE

65        struct memory {
66          char *response;
67          size_t size;
68        };
69
70        static size_t cb(void *data, size_t size, size_t nmemb, void *userp)
71        {
72          size_t realsize = size * nmemb;
73          struct memory *mem = (struct memory *)userp;
74
75          char *ptr = realloc(mem->response, mem->size + realsize + 1);
76          if(ptr == NULL)
77            return 0;  /* out of memory! */
78
79          mem->response = ptr;
80          memcpy(&(mem->response[mem->size]), data, realsize);
81          mem->size += realsize;
82          mem->response[mem->size] = 0;
83
84          return realsize;
85        }
86
87        struct memory chunk = {0};
88
89        /* send all data to this function  */
90        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, cb);
91
92        /* we pass our 'chunk' struct to the callback function */
93        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
94

AVAILABILITY

96       Support  for  the CURL_WRITEFUNC_PAUSE return code was added in version
97       7.18.0.
98

RETURN VALUE

100       This will return CURLE_OK.
101

SEE ALSO

103       CURLOPT_WRITEDATA(3),   CURLOPT_READFUNCTION(3),    CURLOPT_HEADERFUNC‐
104       TION(3),
105
106
107
108libcurl 7.85.0                   May 17, 2022         CURLOPT_WRITEFUNCTION(3)
Impressum