Skip to content

Commit

Permalink
Delete content-encoding header after decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
tanguilp committed Jun 19, 2023
1 parent 0b4e336 commit 639c035
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -825,10 +825,18 @@ defmodule Req.Steps do
iex> response = Req.get!("https://httpbin.org/gzip")
iex> List.keyfind(response.headers, "content-encoding", 0)
{"content-encoding", "gzip"}
nil
iex> response.body["gzipped"]
true
iex> response = Req.get!("https://httpbin.org/gzip", raw: true)
iex> List.keyfind(response.headers, "content-encoding", 0)
{"content-encoding", "gzip"}
iex> response.body
<<31, 139, 8, 0, 152, 130, 144, 100, 2, 255, 61, 143, 61, 111, 195, 32, 16, 134,
119, 255, 10, 196, 108, 168, 49, 254, 8, 145, 58, 120, 136, 210, 174, 85, 42,
117, 117, 224, 130, 145, 26, 112, 240, 117, 113, 148, 255, 94, 112, 164, ...>>
If the [brotli] package is installed, Brotli is also supported:
Mix.install([
Expand Down Expand Up @@ -857,7 +865,16 @@ defmodule Req.Steps do

def decompress_body({request, response}) do
compression_algorithms = get_content_encoding_header(response.headers)
{request, update_in(response.body, &decompress_body(&1, compression_algorithms))}

response =
Req.Response.new(
status: response.status,
headers: List.keydelete(response.headers, "content-encoding", 0),
body: decompress_body(response.body, compression_algorithms),
private: response.private
)

{request, response}
end

defp decompress_body(body, algorithms) do
Expand Down

0 comments on commit 639c035

Please sign in to comment.