11

I added curl_easy_setopt(client, CURLOPT_ENCODING, "gzip"); to my code.

I expected curl to cause the server to send compressed data AND to decompress it.

Actually i see in HTTP header that the data is compressed (Vary: Accept-Encoding Content-Encoding: gzip), but curl doesn't decompress it for me.

Is there an additional command I should use for this?

enb081
  • 3,831
  • 11
  • 43
  • 66
tzviya
  • 537
  • 1
  • 4
  • 14

2 Answers2

17

Note that this option has been renamed to CURLOPT_ACCEPT_ENCODING.

As stated by the documentation:

Sets the contents of the Accept-Encoding: header sent in a HTTP request, and enables decoding of a response when a Content-Encoding: header is received.

So it does decode (i.e decompress) the response. Three encoding are supported: "identity" (does nothing), "zlib" and "gzip". Alternatively you can pass an empty string which creates an Accept-Encoding: header containing all supported encodings.

At last, httpbin is handy to test it out as it contains a dedicated endpoint that returns gzip content. Here's an example:

#include <curl/curl.h>

int
main(void)
{
  CURLcode rc;
  CURL *curl;

  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/gzip");
  curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

  rc = curl_easy_perform(curl);

  curl_easy_cleanup(curl);

  return (int) rc;
}

It sends:

GET /gzip HTTP/1.1
Host: httpbin.org
Accept: */*
Accept-Encoding: gzip

And gets as response:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Content-Type: application/json
...

And a JSON response (thus decompressed) is written on stdout.

deltheil
  • 15,496
  • 2
  • 44
  • 64
-4

c++ CURL library does not compress/decompress your data. you must do it yourself.

        CURL *curl = curl_easy_init();

        struct curl_slist *headers=NULL;
        headers = curl_slist_append(headers, "Accept: application/json");
        headers = curl_slist_append(headers, "Content-Type: application/json");
        headers = curl_slist_append(headers, "Content-Encoding: gzip");

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers );
        curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, zipped_data.data() );
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, zipped_data.size() );
  • 3
    Wrong for downloads (as the previous comment explains), correct for uploads (as HTTP doesn't compress uploads). – Daniel Stenberg Jun 30 '15 at 11:52
  • 3
    If you use CURLOPT_HTTPHEADER to specify the Accept-Encoding header, libcurl will not do any compressioin/decompression. But if you use CURLOPT_ACCEPT_ENCODING libcurl will do everything for you. See http://curl.haxx.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html – HussoM Oct 27 '15 at 13:00