hackney
hackney copied to clipboard
Encoding float form data
Hi,
version: 1.15.1
Trying to post data containing some float, I encountered the following crash:
(hackney) /home/melmoth/elixir/test_poison/deps/hackney/src/hackney_bstr.erl:35: :hackney_bstr.to_binary/1 (hackney) /home/melmoth/elixir/test_poison/deps/hackney/src/hackney_url.erl:313: :hackney_url.urlencode/2 (hackney) /home/melmoth/elixir/test_poison/deps/hackney/src/hackney_url.erl:373: :hackney_url.qs/3 (hackney) /home/melmoth/elixir/test_poison/deps/hackney/src/hackney_request.erl:312: :hackney_request.encode_form/1 (hackney) /home/melmoth/elixir/test_poison/deps/hackney/src/hackney_request.erl:320: :hackney_request.handle_body/4 (hackney) /home/melmoth/elixir/test_poison/deps/hackney/src/hackney_request.erl:83: :hackney_request.perform/2 (hackney) /home/melmoth/elixir/test_poison/deps/hackney/src/hackney.erl:373: :hackney.send_request/2 (httpoison) lib/httpoison/base.ex:786: HTTPoison.Base.request/6
Quickly adding the following matching declaration in hackney_bstr fixed the problem:
to_binary(V) when is_integer(V) -> list_to_binary(integer_to_list(V));
But maybe there is a good reason not to include it ? (aside than the usual float approximation)
Thanks for your work guys !! :)
Just tried to add support for this .. erlang's float to string logic is ... interesting:
8> float_to_list(1.33, [{decimals, 15}, compact]).
"1.33"
9> float_to_list(1.33, [{decimals, 20}, compact]).
"1.33000000000000007105"
using binary is maybe better. I will test.
9> float_to_list(1.33, [{decimals, 20}, compact]). "1.33000000000000007105"
Seems correct, what's interesting about it?
You can use io_lib_format:fwrite_g/1.