1--- 2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al. 3SPDX-License-Identifier: curl 4Title: CURLOPT_WRITEFUNCTION 5Section: 3 6Source: libcurl 7See-also: 8 - CURLOPT_HEADERFUNCTION (3) 9 - CURLOPT_READFUNCTION (3) 10 - CURLOPT_WRITEDATA (3) 11--- 12 13# NAME 14 15CURLOPT_WRITEFUNCTION - callback for writing received data 16 17# SYNOPSIS 18 19~~~c 20#include <curl/curl.h> 21 22size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata); 23 24CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEFUNCTION, write_callback); 25~~~ 26 27# DESCRIPTION 28 29Pass a pointer to your callback function, which should match the prototype 30shown above. 31 32This callback function gets called by libcurl as soon as there is data 33received that needs to be saved. For most transfers, this callback gets called 34many times and each invoke delivers another chunk of data. *ptr* points to the 35delivered data, and the size of that data is *nmemb*; *size* is always 1. 36 37The data passed to this function is not null-terminated. 38 39The callback function is passed as much data as possible in all invokes, but 40you must not make any assumptions. It may be one byte, it may be 41thousands. The maximum amount of body data that is be passed to the write 42callback is defined in the curl.h header file: *CURL_MAX_WRITE_SIZE* (the 43usual default is 16K). If CURLOPT_HEADER(3) is enabled, which makes header 44data get passed to the write callback, you can get up to 45*CURL_MAX_HTTP_HEADER* bytes of header data passed into it. This usually means 46100K. 47 48This function may be called with zero bytes data if the transferred file is 49empty. 50 51Set the *userdata* argument with the CURLOPT_WRITEDATA(3) option. 52 53Your callback should return the number of bytes actually taken care of. If 54that amount differs from the amount passed to your callback function, it 55signals an error condition to the library. This causes the transfer to get 56aborted and the libcurl function used returns *CURLE_WRITE_ERROR*. 57 58You can also abort the transfer by returning CURL_WRITEFUNC_ERROR (added in 597.87.0), which makes *CURLE_WRITE_ERROR* get returned. 60 61If the callback function returns CURL_WRITEFUNC_PAUSE it pauses this 62transfer. See curl_easy_pause(3) for further details. 63 64Set this option to NULL to get the internal default function used instead of 65your callback. The internal default function writes the data to the FILE * 66given with CURLOPT_WRITEDATA(3). 67 68This option does not enable HSTS, you need to use CURLOPT_HSTS_CTRL(3) to 69do that. 70 71# DEFAULT 72 73libcurl uses 'fwrite' as a callback by default. 74 75# PROTOCOLS 76 77For all protocols 78 79# EXAMPLE 80 81~~~c 82#include <stdlib.h> /* for realloc */ 83#include <string.h> /* for memcpy */ 84 85struct memory { 86 char *response; 87 size_t size; 88}; 89 90static size_t cb(void *data, size_t size, size_t nmemb, void *clientp) 91{ 92 size_t realsize = size * nmemb; 93 struct memory *mem = (struct memory *)clientp; 94 95 char *ptr = realloc(mem->response, mem->size + realsize + 1); 96 if(!ptr) 97 return 0; /* out of memory! */ 98 99 mem->response = ptr; 100 memcpy(&(mem->response[mem->size]), data, realsize); 101 mem->size += realsize; 102 mem->response[mem->size] = 0; 103 104 return realsize; 105} 106 107int main(void) 108{ 109 struct memory chunk = {0}; 110 CURLcode res; 111 CURL *curl = curl_easy_init(); 112 if(curl) { 113 /* send all data to this function */ 114 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb); 115 116 /* we pass our 'chunk' struct to the callback function */ 117 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); 118 119 /* send a request */ 120 res = curl_easy_perform(curl); 121 122 /* remember to free the buffer */ 123 free(chunk.response); 124 125 curl_easy_cleanup(curl); 126 } 127} 128~~~ 129 130# AVAILABILITY 131 132Support for the CURL_WRITEFUNC_PAUSE return code was added in version 7.18.0. 133 134# RETURN VALUE 135 136This returns CURLE_OK. 137