Drop the spaces between Japanese text and inline code or alphanumerics in the tour and the few other pages that still had them, and reword sentences that read awkwardly. The client compression page now lists Zstd alongside zlib and Brotli.
1.8 KiB
title, order, status
| title | order | status |
|---|---|---|
| C15. Enable Compression | 15 | draft |
cpp-httplib supports compression when sending and decompression when receiving. You just need to build it with zlib, Brotli, or Zstd enabled.
Build-time setup
To use compression, define these macros before including httplib.h:
#define CPPHTTPLIB_ZLIB_SUPPORT // gzip / deflate
#define CPPHTTPLIB_BROTLI_SUPPORT // brotli
#define CPPHTTPLIB_ZSTD_SUPPORT // zstd
#include <httplib.h>
You'll also need to link against zlib, brotli, or zstd, whichever you enabled.
Compress the request body
httplib::Client cli("https://api.example.com");
cli.set_compress(true);
std::string big_payload = build_payload();
auto res = cli.Post("/api/data", big_payload, "application/json");
With set_compress(true), the body of POST or PUT requests is compressed before sending. The encoding is the first one enabled in your build, in the order Brotli, gzip, Zstd. The server needs to handle compressed bodies too.
Decompress the response
httplib::Client cli("https://api.example.com");
cli.set_decompress(true); // on by default
auto res = cli.Get("/api/data");
std::cout << res->body << std::endl;
With set_decompress(true), the client automatically decompresses responses that arrive with Content-Encoding: gzip or similar. res->body contains the decompressed data.
It's on by default, so normally you don't need to do anything. Set it to false only if you want the raw compressed bytes.
Warning: If you build without any compression library,
set_compress(true)leaves the request uncompressed. And a response compressed with an encoding your build lacks makes the request fail withError::UnsupportedContentEncoding. If compression isn't working, check the macro definitions first.