Skip to content

Commit

Permalink
Merge pull request #144 from pow-auth/set-content-length
Browse files Browse the repository at this point in the history
Set content-length header in :httpc requests
  • Loading branch information
danschultzer authored Nov 22, 2023
2 parents dbba6c5 + a681227 commit 8b3fb85
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()
[{~c"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 8b3fb85

Please sign in to comment.