hackney icon indicating copy to clipboard operation
hackney copied to clipboard

Encoding float form data

Open freandre opened this issue 6 years ago • 4 comments

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 !! :)

freandre avatar Jun 13 '19 20:06 freandre

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"

pdelanauze avatar Aug 06 '19 18:08 pdelanauze

using binary is maybe better. I will test.

benoitc avatar Dec 04 '19 22:12 benoitc

9> float_to_list(1.33, [{decimals, 20}, compact]).
"1.33000000000000007105"

Seems correct, what's interesting about it?

OvermindDL1 avatar Dec 05 '19 16:12 OvermindDL1

You can use io_lib_format:fwrite_g/1.

ericmj avatar Dec 05 '19 21:12 ericmj