hackney icon indicating copy to clipboard operation
hackney copied to clipboard

hackney_url:qs error

Open dcy opened this issue 7 years ago • 5 comments

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'

dcy avatar Sep 30 '16 08:09 dcy

how would you expect to have it encoded? As multiple k/v ?

benoitc avatar Oct 05 '16 18:10 benoitc

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.

edgurgel avatar Oct 05 '16 20:10 edgurgel

@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.

dcy avatar Oct 08 '16 02:10 dcy

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.

benoitc avatar Oct 09 '16 05:10 benoitc

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]">>

zeek0x avatar Dec 21 '19 08:12 zeek0x