Sortix cross-nightly manual
This manual documents Sortix cross-nightly. You can instead view this document in the latest official manual.
CURLOPT_WRITEFUNCTION(3) | Library Functions Manual | CURLOPT_WRITEFUNCTION(3) |
NAME
CURLOPT_WRITEFUNCTION - callback for writing received dataSYNOPSIS
#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
Pass a pointer to your callback function, which should match the prototype shown above.DEFAULT
fwrite(3)PROTOCOLS
This functionality affects all supported protocolsEXAMPLE
#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 = size * 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 res;
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 */
res = curl_easy_perform(curl);
/* remember to free the buffer */
free(chunk.response);
curl_easy_cleanup(curl);
}
}
HISTORY
Support for the CURL_WRITEFUNC_PAUSE return code was added in version 7.18.0.AVAILABILITY
Added in curl 7.1RETURN VALUE
This returns CURLE_OK.SEE ALSO
CURLOPT_HEADERFUNCTION(3), CURLOPT_READFUNCTION(3), CURLOPT_WRITEDATA(3)2024-11-24 | libcurl |