URL encode in Erlang and Cyrillic

若如初见. 提交于 2019-12-06 08:15:42

问题


I'm using standard httpc client in erlang, for sending/receiving request to some service. Sometimes I have a Cyrillic path in my URL. How can I URL-encode Cyrillic URLs?

Thank you.


回答1:


~/erl$erl
Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.8.5  (abort with ^G)
1> Encoded = edoc_lib:escape_uri("абвгдеё").
"%c0%b0%c0%b1%c0%b2%c0%b3%c0%b4%c0%b5%c1%91"
2> http_uri:decode(Encoded).
[192,176,192,177,192,178,192,179,192,180,192,181,193,145]

You may use list_to_binary to use as binary.




回答2:


I had some troubles with the escape_uri from edoc lib.

So I ended up writing one that works with utf-8:

https://stackoverflow.com/a/12648499/113112

https://gist.github.com/3796470

Ex.

Eshell V5.9.1  (abort with ^G)

1> c(encode_uri_rfc3986).
{ok,encode_uri_rfc3986}

2> encode_uri_rfc3986:encode("абвгдеё").
"%d0%b0%d0%b1%d0%b2%d0%b3%d0%b4%d0%b5%d1%91"

3> edoc_lib:escape_uri("абвгдеё").
"%c0%b0%c0%b1%c0%b2%c0%b3%c0%b4%c0%b5%c1%91" # output wrong: À°À±À²À³À´ÀµÁ‘


来源:https://stackoverflow.com/questions/10335682/url-encode-in-erlang-and-cyrillic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!