Decodes response body based on the detected format.
To decode other formats, or to add support for custom ones, use the :decoders option.
Built-in decoders
| Format | Decoder | Enabled | Streaming Decoding |
|---|---|---|---|
:json, :json_api | Req.JSON | ✓ | |
:ndjson | Req.NDJSON | ✓ | ✓ |
:sse | Req.SSE | ✓ | ✓ |
:zip | Req.ZIP | ||
:tar, :tgz | Req.Tar | ||
:gz | Req.Gzip | ✓ | |
:zst | Req.Zstd (requires Erlang/OTP 28+) | ✓ | |
:csv | Req.CSV (requires nimble_csv) |
The format is determined by the response content-type header. See MIME for registering
content-type/format mapping.
Decompression Bombs
The archive and compression decoders (:zip, :tar, :tgz, :gz, and :zst) decompress
the whole response body into memory with no size limit, so a small response can expand to
many gigabytes. For this reason they are not enabled by default; only opt into them via
the :decoders option for endpoints you trust.
Request Options
:decoders- the list of decoders to use. Defaults to[:json, :json_api, :ndjson, :sse].Each element is either:
a format (atom) handled by a built-in decoder, e.g.
:jsonor:zip;a
{format, decoder}or{content_type, decoder}tuple, whereformatis an atom e.g.::json,content_typeis a binary e.g.:application/x-amz-json-1.0, anddecoderis one of:another format (atom), to reuse a built-in decoder, e.g.
decoders: [json5: :json].a 1-arity function that returns
{:ok, term}or{:error, exception}, e.g.:decoders: [{"text/calendar", &{:ok, ICal.from_ics(&1)}}]
Setting
:decodersreplaces the default, so include:jsonif you still want JSON decoded:# handles json, zip, and tar: Req.new(decoders: [:json, :zip, :tar])Set
:decoderstofalseto disable all decoding, including JSON.:decode_body- if set tofalse, disables automatic response body decoding. Defaults totrue.:raw- if set totrue, disables response body decoding. Defaults tofalse.Note: setting
raw: truealso disables response body decompression.
Examples
Decode JSON:
iex> response = Req.get!("https://httpbingo.org/json")
...> response.body["slideshow"]["title"]
"Sample Slide Show"Decode a ZIP archive (opt-in):
iex> response = Req.get!("https://example.com/archive.zip", decoders: [:zip])
...> response.body["file.txt"]
"contents"