libcurl HTTP download

The GET method is the default method libcurl uses when a HTTP URL is requested
and no particular other method is asked for. It asks the server for a
particular resource—the standard HTTP download request:

  1. easy = curl_easy_init();
  2. curl_easy_setopt(easy, CURLOPT_URL, "http://example.com/");
  3. curl_easy_perform(easy);

Since options set in an easy handle are sticky and remain until changed, there
may be times when you have asked for another request method than GET and then
want to switch back to GET again for a subsequent request. For this purpose,
there’s the CURLOPT_HTTPGET option:

  1. curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);

Download headers too

A HTTP transfer also includes a set of response headers. Response headers are
metadata associated with the actual payload, called the response body. All
downloads will get a set of headers too, but when using libcurl you can select
whether you want to have them downloaded (seen) or not.

You can ask libcurl to pass on the headers to the same “stream” as the regular
body is, by using CURLOPT_HEADER:

  1. easy = curl_easy_init();
  2. curl_easy_setopt(easy, CURLOPT_HEADER, 1L);
  3. curl_easy_setopt(easy, CURLOPT_URL, "http://example.com/");
  4. curl_easy_perform(easy);

Or you can opt to store the headers in a separate download file, by relying on
the default behaviors of the write and header
callbacks
:

  1. easy = curl_easy_init();
  2. FILE *file = fopen("headers", "wb");
  3. curl_easy_setopt(easy, CURLOPT_HEADERDATA, file);
  4. curl_easy_setopt(easy, CURLOPT_URL, "http://example.com/");
  5. curl_easy_perform(easy);
  6. fclose(file);

If you only want to casually browse the headers, you may even be happy enough
with just setting verbose mode while developing as that will show both outgoing
and incoming headers sent to stderr:

  1. curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);