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 which causes the post to fail when parsed by the remote service.
Is there a function in Erlang that is similar to CGI.escape
in Ruby for this purpose?
You can find here the YAWS url_encode and url_decode routines
They are fairly straightforward, although comments indicate the encode is not 100% complete for all punctuation characters.
If someone need encode uri that works with utf-8 in erlang:
https://gist.github.com/3796470
Ex.
Here's a "fork" of the
edoc_lib:escape_uri
function that improves on the UTF-8 support and also supports binaries.Note that, because of the use of unicode:characters_to_binary it'll only work in R13 or newer.
Example usage is:
We send out a request with escaped query parameter and see that we get back the correct Unicode codepoint.
At least in R15 there is http_uri:encode/1 which does the job. I would also not recommend using edoc_lib:escape_uri as its translating an '=' to a %3d instead of a %3D which caused me some trouble.
To answer my own question...I found this lib in ibrowse!
http://www.erlware.org/lib/5.6.3/ibrowse-1.4/ibrowse_lib.html#url_encode-1
URL-encodes a string based on RFC 1738. Returns a flat list.
I guess I can use this to do the encoding and still use http:
Here's a simple function that does the job. It's designed to work directly with inets httpc.
Example usage: