How do you URL encode parameters in Erlang?

前端 未结 8 1113
清歌不尽
清歌不尽 2020-12-31 01:17

I\'m using httpc:request to post some data to a remote service. I have the post working but the data in the body() of the post comes through as is, without any URL-encoding

8条回答
  •  [愿得一人]
    2020-12-31 01:45

    Here's a "fork" of the edoc_lib:escape_uri function that improves on the UTF-8 support and also supports binaries.

    escape_uri(S) when is_list(S) ->
        escape_uri(unicode:characters_to_binary(S));
    escape_uri(<>) when C >= $a, C =< $z ->
        [C] ++ escape_uri(Cs);
    escape_uri(<>) when C >= $A, C =< $Z ->
        [C] ++ escape_uri(Cs);
    escape_uri(<>) when C >= $0, C =< $9 ->
        [C] ++ escape_uri(Cs);
    escape_uri(<>) when C == $. ->
        [C] ++ escape_uri(Cs);
    escape_uri(<>) when C == $- ->
        [C] ++ escape_uri(Cs);
    escape_uri(<>) when C == $_ ->
        [C] ++ escape_uri(Cs);
    escape_uri(<>) ->
        escape_byte(C) ++ escape_uri(Cs);
    escape_uri(<<>>) ->
        "".
    
    escape_byte(C) ->
        "%" ++ hex_octet(C).
    
    hex_octet(N) when N =< 9 ->
        [$0 + N];
    hex_octet(N) when N > 15 ->
        hex_octet(N bsr 4) ++ hex_octet(N band 15);
    hex_octet(N) ->
        [N - 10 + $a].
    

    Note that, because of the use of unicode:characters_to_binary it'll only work in R13 or newer.

    Example usage is:

    9> httpc:request("http://httpbin.org/get?q=" ++ mylib_app:escape_uri("☺")).
    {ok,{{"HTTP/1.1",200,"OK"},
         [{"connection","keep-alive"},
          {"date","Sat, 09 Nov 2019 21:51:54 GMT"},
          {"server","nginx"},
          {"content-length","178"},
          {"content-type","application/json"},
          {"access-control-allow-credentials","true"},
          {"access-control-allow-origin","*"},
          {"referrer-policy","no-referrer-when-downgrade"},
          {"x-content-type-options","nosniff"},
          {"x-frame-options","DENY"},
          {"x-xss-protection","1; mode=block"}],
         "{\n  \"args\": {\n    \"q\": \"\\u263a\"\n  }, \n  \"headers\": {\n    \"Host\": \"httpbin.org\"\n  }, \n  \"origin\": \"11.111.111.111, 11.111.111.111\", \n  \"url\": \"https://httpbin.org/get?q=\\u263a\"\n}\n"}}
    
    

    We send out a request with escaped query parameter and see that we get back the correct Unicode codepoint.

提交回复
热议问题