CURLOPT_WRITEFUNCTION(3)
π CURLOPT_WRITEFUNCTION(3) β the libcurl library functions manual, bestie edition π
okay so this is a page from the libcurl library functions manual, and it's literally about CURLOPT_WRITEFUNCTION β the callback that catches your data as it comes in. sit down, we're doing this together π«Ά
β¨ NAME β¨
CURLOPT_WRITEFUNCTION β the callback for writing received data. that's it, that's the whole vibe π
πΈ SYNOPSIS πΈ
#include <curl/curl.h>
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEFUNCTION, write_callback);
π¦ DESCRIPTION π¦
so you pass in a pointer to your callback function, and it needs to match that prototype up there exactly β no improvising, bestie, the signature is the signature π
this callback gets called by libcurl the second there's data received that needs to be saved. for most transfers it gets called many times, and every single invoke delivers another chunk of data. ptr points at the delivered data, and the size of that data is nmemb; size is always 1. yes always. don't come for me, that's just how it is π§
heads up: the data passed to this function is not null-terminated. do not go treating it like a cute little C string, it will not be one π
the callback gets as much data as possible in all invokes, but you must not make any assumptions β it may be one byte, it may be thousands. lowkey chaotic but that's the deal. the maximum amount of body data passed to the write callback is defined in the curl.h header file: CURL_MAX_WRITE_SIZE (the usual default is 16K). and if CURLOPT_HEADER(3) is enabled β which makes header data get passed to the write callback too β you can get up to CURL_MAX_HTTP_HEADER bytes of header data passed into it. that usually means 100K. big mood π₯
this function may be called with zero bytes of data if the transferred file is empty. iconic of an empty file honestly π
you set the userdata argument with the CURLOPT_WRITEDATA(3) option.
your callback should return the number of bytes actually taken care of. if that amount differs from the amount passed to your callback function, that signals an error condition to the library β which causes the transfer to get aborted, and the libcurl function used returns CURLE_WRITE_ERROR. so like, count correctly please π
you can also abort the transfer by returning CURL_WRITEFUNC_ERROR (added in 7.87.0), which makes CURLE_WRITE_ERROR get returned. same energy, different path β¨
and if the callback function returns CURL_WRITEFUNC_PAUSE it pauses this transfer. see curl_easy_pause(3) for further details, that's a whole other page and she's got lore π
set this option to NULL to get the internal default function used instead of your callback. that internal default function writes the data to the FILE * given with CURLOPT_WRITEDATA(3). convenient, ngl π
real talk: this option does not enable HSTS. you need to useCURLOPT_HSTS_CTRL(3)to do that. don't be out here settingCURLOPT_WRITEFUNCTIONand expecting HSTS, that's not her job π
π DEFAULT π
π PROTOCOLS π
this functionality affects all supported protocols. all of them. every single one. periodt π
β EXAMPLE β
okay here's a whole working example, look at her go:
#include <stdlib.h> /* for realloc */
#include <string.h> /* for memcpy */
struct memory {
char *response;
size_t size;
};
static size_t cb(char *data, size_t size, size_t nmemb, void *clientp)
{
size_t realsize = nmemb;
struct memory *mem = (struct memory *)clientp;
char *ptr = realloc(mem->response, mem->size + realsize + 1);
if(!ptr)
return 0; /* out of memory */
mem->response = ptr;
memcpy(&(mem->response[mem->size]), data, realsize);
mem->size += realsize;
mem->response[mem->size] = 0;
return realsize;
}
int main(void)
{
struct memory chunk = { 0 };
CURLcode result;
CURL *curl = curl_easy_init();
if(curl) {
/* send all data to this function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
/* send a request */
result = curl_easy_perform(curl);
/* remember to free the buffer */
free(chunk.response);
curl_easy_cleanup(curl);
}
}
the struct collects your bytes, the callback grows the buffer with realloc, memcpy copies it in, and you null-terminate at the end so you can actually print it later. main character energy fr fr π
π HISTORY π
support for the CURL_WRITEFUNC_PAUSE return code was added in version 7.18.0. she's been pausing transfers since way back, we stan a veteran πΈ
π AVAILABILITY π
added in curl 7.1. ancient. legendary. still here π«Ά
π₯ RETURN VALUE π₯
this returns CURLE_OK. that's it, that's the return value, no drama β¨
π SEE ALSO π
and that's the page, bestie π huge kisses to libcurl and to the 2026-10-12 date stamp stamped all over this thing β this page came straight out of the libcurl manual, version and date and all. now go write your callback and treat nmemb right ππ«Ά
π¦ where this page comes from
- fedora 44: libcurl-devel 8.18.0-14.fc44 Β· curl
- fedora 44: libcurl-devel 8.18.0-14.fc44 Β· curl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms