Skip to content

Commit

Permalink
Set content length header
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Nov 22, 2023
1 parent dbba6c5 commit 05157d7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v0.2.9 (TBA)

- Fixed bug where `Req` was not used by default if included in project
- `Assent.Strategy.Httpc.request/5` now sets content length header

## v0.2.8 (2023-11-19)

Expand Down
12 changes: 12 additions & 0 deletions lib/assent/http_adapter/httpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ defmodule Assent.HTTPAdapter.Httpc do
defp do_httpc_request(url, body, headers) do
{content_type, headers} = split_content_type_headers(headers)
body = to_charlist(body)
headers = set_content_length_header(headers, body)

{url, headers, content_type, body}
end
Expand All @@ -76,6 +77,17 @@ defmodule Assent.HTTPAdapter.Httpc do
end
end

defp set_content_length_header(headers, body) do
case List.keyfind(headers, ~c"content-length", 0) do
nil ->
length = body |> IO.iodata_length() |> Integer.to_string()
[{'content-length', length} | headers]

_ ->
headers
end
end

defp format_response({:ok, {{_, status, _}, headers, body}}) do
headers =
Enum.map(headers, fn {key, value} ->
Expand Down
2 changes: 2 additions & 0 deletions test/assent/http_adapter/httpc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ defmodule Assent.HTTPAdapter.HttpcTest do
"application/x-www-form-urlencoded"
]

assert Plug.Conn.get_req_header(conn, "content-length") == ["7"]

Plug.Conn.send_resp(conn, 200, "")
end
)
Expand Down

0 comments on commit 05157d7

Please sign in to comment.