Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally process :form bodies to handle nested params #470

Merged
merged 2 commits into from
Mar 2, 2023

Conversation

vereis
Copy link
Contributor

@vereis vereis commented Feb 16, 2023

Ahoy again!

This PR is larger than I expected because when I was looking at the HTTPoison source when I made my issue, I didn't realise I was only looking at the to_curl function 😞, so apologies if this is bigger than expected.

When {:form, body} is passed as a body into HTTPoison, we iterate over all the keys in said body and iff a key contains a nested keyword-list, we apply the following transformation:

[something_nested: [hello: :world]] -> [{"something_nested[hello]", :world}]

The function added supports arbitrarily deeply nested params, and should preserve the order in which things are passed in.

@edgurgel do you think we should extend this to support maps as well? It should be relatively minor (a change in the pattern matches) but then we'd throw the key-ordering guarantees away. WDYT?
edit: Added a second commit that adds support for maps_

Should fix #469

@@ -134,7 +134,7 @@ defmodule HTTPoison.Request do
headers = request.headers |> Enum.map(fn {k, v} -> "-H '#{k}: #{v}'" end) |> Enum.join(" ")

body =
case request.body do
case HTTPoison.Base.maybe_process_form(request.body) do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted to make this a function in HTTPoison.Base so that we can use the same logic for generating curl args vs hackney args

)
end

test "post nested form data when given as map (no order guaranteed)" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important to note that we throw away guarantees re: ordering params when using maps, but that feels like the expected behaviour in Elixir

@edgurgel
Copy link
Owner

Thanks for the PR! I will definitely check it out later this week

@edgurgel edgurgel merged commit 3a436f1 into edgurgel:main Mar 2, 2023
@edgurgel
Copy link
Owner

edgurgel commented Mar 2, 2023

Thanks! I will cut a new release soon 🔜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nested form-encoded params don't work
2 participants