I've looked at using inets:httpc, and it seems to have everything I
need, except that it doesn't appear to allow you to pass it a list of
key/value tuples and have them be encoded into the HTTP request as
application/x-www-form-urlencoded data.
Does anyone know if inets:httpc can do this, and if not whether there
are any libraries I can easily use?
Cheers,
R.
________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questio...@erlang.org
> Does anyone know if there is an API that I can use to post HTTP form data?
>
> I've looked at using inets:httpc, and it seems to have everything I
> need, except that it doesn't appear to allow you to pass it a list of
> key/value tuples and have them be encoded into the HTTP request as
> application/x-www-form-urlencoded data.
>
> Does anyone know if inets:httpc can do this, and if not whether there
> are any libraries I can easily use?
>
>
Mochiweb provides a Request "object" to your callback function, and that has
a parse_post/0 function, which'll give you back a proplist of key/value
pairs from your POST body (and you can use parse_qs/0 for GET options after
the ? on the url).
Haven't used inets in a while.
Robby
httpc:request(post, {Url, [], "application/x-www-form-urlencoded, "key=value&key2=value2}, [], []).
If required you could use edoc:escape_uri(String) for the individual values.
Be sure to start inets before using httpc.
Tino
Yes, I discovered that shortly after sending the email; prompting me
to write the following function, rather than take on an external
dependency, which I'm a little loathed to do for something so small:
url_encode(Data) ->
url_encode(Data,"").
url_encode([],Acc) ->
Acc;
url_encode([{Key,Value}|R],"") ->
url_encode(R, edoc_lib:escape_uri(Key) ++ "=" ++
edoc_lib:escape_uri(Value));
url_encode([{Key,Value}|R],Acc) ->
url_encode(R, Acc ++ "&" ++ edoc_lib:escape_uri(Key) ++ "=" ++
edoc_lib:escape_uri(Value)).
Thanks to everyone for your help,
R.
URI escaping and application/x-www-form-urlencoded escaping are not
exactly the same thing though.
There's an implementation of both here:
http://github.com/tim/erlang-percent-encoding
I'd appreciate any feedback on the correctness of the code/tests.
> Yes, I discovered that shortly after sending the email; prompting me
> to write the following function, rather than take on an external
> dependency, which I'm a little loathed to do for something so small:
Agreed. I usually copy/paste bits into other projects, for example:
http://github.com/tim/erlang-webfinger/blob/master/src/webfinger.erl#L65-82
Cheers,
Tim