Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use httpotion behind the proxy?

Tags:

proxy

elixir

(Edit: I cleared this issue by using HTTPoison's get! function.

HTTPoison.start
HTTPoison.get!("httpbin.org/get", [], [{:proxy, {"proxy.mydomain.com", 8080}}])

I'm a newbie for using elixir. I tried the sample app on httpotion as the first step.

iex> response = HTTPotion.get "httpbin.org/get"

However, it couldn't reach to the site behind the proxy.

iex(1)> res = HTTPotion.get "httpbin.org/get"
** (HTTPotion.HTTPError) nxdomain
    (httpotion) lib/httpotion.ex:195: HTTPotion.handle_response/1

Without proxy, it successfully works like this;

iex(1)> res = HTTPotion.get "httpbin.org/get"
%HTTPotion.Response{body: "{\n  \"args\": {}, \n  \"headers\": {\n        \"Content-Length\": \"0\", \n    \"Host\": \"httpbin.org\"\n  }, \n  \"origin\": \"191.238.84.51\", \n  \"url\": \"http://httpbin.org/get\"\n}\n",
 headers: ["Access-Control-Allow-Credentials": "true",...

I tried to set proxy parameters by reading ibrowse which httpotion depends on, like;

req = HTTPotion.get("httpbin.org/get", [{:proxy_host, "proxy.mydomain.com"}, {:proxy_port, 8080}])

But the result is same.

How can I set the proxy parameters for httpotion? Or are there any replacement library on elixir for HTTP access which can handle proxies?

My environment is Ubuntu 14.04.2 and environment variables (http_proxy, https_proxy, HTTP_PROXY and HTTPS_PROXY) are set correctly.

like image 453
HirofumiTamori Avatar asked May 16 '15 05:05

HirofumiTamori


2 Answers

Take a look at httpoison tests :D

here is how you do a get request with proxy:

HTTPoison.get!("http://www.google.com", [], [{:proxy, "proxy.company.address:port"}])
like image 67
user601836 Avatar answered Sep 21 '22 16:09

user601836


I had just figured this out myself by reading the source, but I now notice it's documented in the latest README...

Short version (since this answer came up earlier in a Google search than the README) is you need to pass the params direct to ibrowse, you do this using the :ibrowse option, then also notice ibrowse generally takes character lists

So, example:

HTTPotion.get "httpbin.org/get", [ ibrowse: [ proxy_host: 'some.host', proxy_port: 8080 ] ]

Note, httpotion doesn't seem to trap exceptions terribly well in it's non "!" versions of functions... Failing to use char lists or similar will cause all kinds of hard to comprehend exceptions...

like image 23
Hi Fly Avatar answered Sep 21 '22 16:09

Hi Fly