How to use httpc:request if the URL is a Unicode string?
get_songs(Findstring) ->
application:start(inets),
application:start(crypto),
application:start(public_key),
application:start(ssl),
Uf = normalize(Findstring),
Ufs = unicode:characters_to_list(Uf),
Token= "e...2",
Req1="https://api.vk.com/method/audio.search?q="++Ufs++"&access_token="++Token,
case httpc:request(get, {Req1,[]}, [], []) of
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, Body}} ->
Result = io_lib:format("~ts",[Body]),
Result;
Resp ->
Result = io_lib:format("response: ~p ~n", [Resp]),
Result
end.
If I send Findstring as Latin1, the code working correctly. But if Findstring is in Unicode, I get an error.
crasher:
initial call: httpc_handler:init/1
pid: <0.172.0>
registered_name: []
exception error: bad argument
in function iolist_to_binary/1
called as iolist_to_binary(["GET"," ",
[47,109,101,116,104,111,100,47,97,117,
100,105,111,46,115,101,97,114,99,104,63,
1083,1086,1083,38,97,99,99,101,115,115,
95,116,111,107,101,110,61,101,50,102,50,
98,55,53,100,101,50,101,101,52,55,51,48,
101,50,101,101,52,55,51,48,97,51,101,50,
100,102,102,55,54,57,101,101,50,101,102,
101,50,101,49,48,53,48,101,98,50,53,97,
101,50,49,51,50,98,98,55,98,100,56,57,
101,99,55,56,53,101,99,50],
" ","HTTP/1.1","\r\n",
"te: \r\nhost: api.vk.com\r\nconnection: keep-alive\r\n",
"\r\n",[]])
You have to urlencode the query string parameters,,,
It might seem strange that Erlang HTTP(S) modules do not contain that a useful routine, but may be related to module arrangement (I'm not an Erlang guru)