Gzip swirl decompression data

I added curl_easy_setopt(client, CURLOPT_ENCODING, "gzip"); into your code.

I expected curl to make the server send compressed data and unpack it.

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

Is there an extra command I should use for this?

+7
c ++ curl libcurl
source share
2 answers

Note that this parameter has been renamed CURLOPT_ACCEPT_ENCODING .

As stated in the documentation:

Sets the contents of the Accept-Encoding: header sent in the HTTP request and allows you to decode the response when the Content-Encoding header is received:

Thus, it decodes (i.e. unpacks) the response. Three encodings are supported: "identity" (does nothing), "zlib" and "gzip" . Alternatively, you can pass an empty string that creates the Accept-Encoding: header containing all supported encodings.

Finally, httpbin is convenient to test because it contains a dedicated endpoint that returns the contents of gzip. Here is 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; } 

He sends:

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

And gets the answer:

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

And the JSON response (thus unpacked) is written to stdout.

+10
source share

C ++ The CURL library does not compress or decompress your data. you have to 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() ); 
-one
source share

All Articles