URL encode in Erlang and Cyrillic

末鹿安然 提交于 2019-12-04 15:51:40
~/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.

Renato Albano

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: À°À±À²À³À´ÀµÁ‘
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!