5

(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.

HirofumiTamori
  • 1,125
  • 2
  • 13
  • 27
  • 2
    Maybe ask in HTTPoison project if you can't get an answer here: https://github.com/edgurgel/httpoison – José Valim May 16 '15 at 07:46
  • Thanks, José. I'll ask the question at the project site. I just started learning elixir, still confusing the new syntax like 'atom starting by :'. – HirofumiTamori May 16 '15 at 08:02
  • @HirofumiTamori remember to post an answer to your question if you manage to find a solution through httpoison's issue tracker :). – whatyouhide May 16 '15 at 10:47
  • @whatyouhide Thanks, I found the HTTPoison by the notice of José and got the usage by its document. I'll be careful if I use the issue tracker next time. – HirofumiTamori May 16 '15 at 11:58

2 Answers2

9

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"}])
user601836
  • 3,215
  • 4
  • 38
  • 48
2

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...

Hi Fly
  • 21
  • 1