Compression reduces transferred data size between client and server. httpx automatically advertises for, and decompresses data for known compression formats, gzip and deflate.

HTTPX.get("") #=> response with "content-type" as "gzip" or "deflate", response is decoded automatically

Compressing request payloads

Compressing the request payload is a matter of just adding the content-encoding header with the desired compression format (again, only supports the already mentioned ones):

HTTPX.with(headers: { "content-encoding" => "gzip" }).post("", body:"path/to/file"))

httpx supports “gzip” and “deflate” by default (unsupported encoding formats are ignored).


brotli compression can be activated by loading the :brotli plugin (and installing the brotli gem, which it depends on; caveat: it does not support JRuby).

http = HTTPX.plugin(:brotli)
http.get("") #=> sends "Accept-Encoding: br, gzip, deflate" header

http = HTTPX.plugin(:brotli)
http.with(headers: { "content-encoding" => "br" }).post("", body:"path/to/file"))


By default, httpx will try to decompress response payloads encoded in a supported compression format (i.e. gzip). However, there are cases when, as a user, you don’t want to do it, i.e. store the gzipped payload in a file. In such cases, you can set the :decompress_response_body to true.

response = HTTPX.get("", decompress_response_body: true)


in the same way, you may want to upload a previously-compressed file somewhere. you can then set :decompress_response_body to true.

response =
  headers: { "content-encoding" => "gzip" },
  compress_request_body: true

Next: Error Handling