Home > English, Erlang > HTTP request through proxy in Erlang

HTTP request through proxy in Erlang


Last week I was testing at university  the code I used in the post XML and Erlang. Oddly the first few lines regarding the http request to the direct messages fell into an error. This seemed very strange, because testing the code at home everything was working fine.

The code was more or less the following:

receive_msg(User, Pwd) ->
    application:start(inets),
    Aut = make_auth(User, Pwd),
    http:request(get, {"http://twitter.com/direct_messages.xml",
		       [{"Authorization", Aut}]}, [], []).

After a while I came to the solution: at the university there is a proxy server!

In Erlang, if you are behind a proxy server, all of your requests should specify the proxy address and port, here is the previous function modified to support connection through proxy server:

receive_msg(User, Pwd, Proxy, Port) ->
    application:start(inets),
    http:set_options([{proxy, {{Proxy, Port}, ["localhost"]}}]),
    Aut = make_auth(User, Pwd),
    http:request(get, {"http://twitter.com/direct_messages.xml",
		       [{"Authorization", Aut}]}, [], []).

In the previous lines Proxy is the proxy addres, in my case “proxy.university.it” and Port is simply the port number.

After this correction the code was working fine and I could go further with the testing!

Categories: English, Erlang Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: