hackney
hackney copied to clipboard
hackney_url:qs error
Hi!
I use hackney_url:qa
to urlencode, when the value is a list, it will be wrong.
6> hackney_url:qs([{<<"deviceTokenList">>, [<<"08670650250202362000003019000001">>,<<"sdfsdf">>]}]).
<<"deviceTokenList=08670650250202362000003019000001sdfsdf">>
In Python:
In [4]: urllib.urlencode({'deviceTokenList': ['08670650250202362000003019000001', 'sdfsdf']})
Out[4]: 'deviceTokenList=%5B%2708670650250202362000003019000001%27%2C+%27sdfsdf%27%5D'
how would you expect to have it encoded? As multiple k/v ?
I'm not sure if there's a standard for lists in query strings. So my guess is that Python decided for one approach and that's it.
@benoitc because the other server api require i send urlencoded list. I use
DeviceTokenList = ["08670650250202362000003019000001", "234234324234234"],
lists:flatten(io_lib:format("~p", [DeviceTokenList]))
to format DeviceTokenList, hackeney_url:qs
works.
mmm why closing it? is it fixed? actually i was thinking that instead of flattening the list we should indeed use its representation like python (or others) do. I will make the change.
Hi! @benoitc :smiley:
The hackney_url
functions (e.g. urlencode/2) seem to accept binary()
and string()
as arguments.
To implement the Python approach above, it is convenient to accept [binary()]
instead of string()
. Is there any plan?
-spec urlencode(binary() | [binary()], [qs_opt()]) -> binary().
urlencode(Bins, Opts) when is_list(Bins) ->
Body = lists:foldl(
fun(Bin, Acc) ->
case Acc of
<<"">> -> Bin;
_ -> <<Acc/binary, ",", Bin/binary>>
end
end, <<"">>, Bins),
urlencode(<<"[", Body/binary ,"]">>, Opts);
urlencode(Bin, Opts) ->
Plus = not proplists:get_value(noplus, Opts, false),
Upper = proplists:get_value(upper, Opts, false),
urlencode(hackney_bstr:to_binary(Bin), <<>>, Plus, Upper).
6> hackney_url:urlencode([<<"hoge">>, <<"fuga">>]).
<<"%5bhoge%2cfuga%5d">>
7> hackney_url:urldecode(hackney_url:urlencode([<<"hoge">>, <<"fuga">>])).
<<"[hoge,fuga]">>
8> hackney_url:qs([{<<"deviceTokenList">>, [<<"08670650250202362000003019000001">>,<<"sdfsdf">>]}]).
<<"deviceTokenList=%5b08670650250202362000003019000001%2csdfsdf%5d">>
9> hackney_url:urldecode(hackney_url:qs([{<<"deviceTokenList">>, [<<"08670650250202362000003019000001">>,<<"sdfsdf">>]}])).
<<"deviceTokenList=[08670650250202362000003019000001,sdfsdf]">>