libcurl-video-tutorials

Video tutorials for how to write internet clients using libcurl

View on GitHub

prev: simplest table of contents next: verbose

Video

Youtube: libcurl video tutorial: receive data

Use a write callback

(This example code is inspired by the getinmemory.c example.)

By default libcurl sends received data to stdout, which is rarely what a user wants to do. Instead, applications typically sets up a “write callback” with the CURLOPT_WRITEFUNCTION option to curl_easy_setopt() - and that gets called for every little peace of data that libcurl downloads.

The write callback gets a custom pointer passed to it and in this example we create a struct (struct memory) that we declare in the main function and is called chunk there which we pass a pointer to into the write callback. The pointer is passed on via the CURLOPT_WRITEDATA option.

In the writecallback function, the logic will use realloc() to dynamically expand our receive buffer as more data is delivered, and the new data is appended to the end of that buffer every time it gets invoked.

In the end, if no error occured, the main function will output a short message to stdout saying how many bytes it actually ended up receiving.

setup

Using the Makefile from the setup episode, we build the program and test it.