Home > Net >  C curl callback
C curl callback

Time:10-17

Can anyone inform me why we need a callback in some curl options, like CURLOPT_WRITEFUNCTION?

I used the code below in C , and got the same result with or without a CURLOPT_WRITEFUNCTION callback, so it seems confusing to use a callback.

int main(){
    CURL *curl = curl_easy_init();
    CURLcode response;
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com");
        response=curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }    
}

CodePudding user response:

callback is an option that has it own specific properties that might be usefull for someone who wants to use that properties. see link below https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html

CodePudding user response:

By default, libcurl simply writes the downloaded data to STDOUT.

If you just want to change which FILE* it writes the data to, you can use the CURLOPT_WRITEDATA option.

But, if you want to change how it writes the data, for instance to write to something other than a FILE*, you can use a CURLOPT_WRITEFUNCTION callback.

For instance, see these examples, which use CURLOPT_WRITEFUNCTION to customize downloads:

https://curl.se/libcurl/c/ftpsget.html

https://curl.se/libcurl/c/getinmemory.html

https://curl.se/libcurl/c/http2-pushinmemory.html

  • Related