Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the :httpc erlang module from Elixir

Tags:

elixir

I'm pretty new to elixir and have no erlang experience (or func-y stuff) but that will soon become apparent.. ->

iex(2)> :inets.start()
:ok
iex(3)> :httpc.request(["http://www.erlang.org"])
{:error, :no_scheme}

I have no idea what :no_scheme means. I've googled no_scheme and stuff and I'm sure it's obvious, but I found nothing really. The only vaguely related thing I could find in the erlang docs was ->

iex(4)> :http_uri.scheme_defaults
[http: 80, https: 443, ftp: 21, ssh: 22, sftp: 22, tftp: 69]

or maybe I wasn't conforming to RFC2616 or something... I'm out of ideas (for now).

Elixir and Erlang are super super awesome though so any help in furthering my journey would be appreciated.

Thanks for any help!

I tried it out in erl.

1> inets:start().
ok
2> httpc:request("http://www.erlang.org").
{ok,{{"HTTP/1.1",200,"OK"},
     [{"date","Wed, 20 Nov 2013 23:15:45 GMT"},
      {"server","inets/5.7.1"},
      {"content-length","10385"},
      {"content-type","text/html; charset=utf-8"},
      {"set-cookie",
       "eptic_cookie=erlangorg@hades-3680318586833850369; path=/"}],
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE htm
    etc...

Edit:

Ok so, my biggest problem was I was double quoting "http://www.erlang.org" the strings and I'm guess elixir -> erlang takes them in as single quoted like parrotys answer 'http://www.erlang.org'.

no_scheme still pretty cryptic. All I could find was stuff relating to redirects and the RFC implementation. I've just decided to consider it a generic error, something to do with the url..

Edit 2:

There's a blog post here by someone that explains it well. Link

Erlang atoms like database become :database and a local variable like PgConn in the Erlang version becomes pg_conn in Elixir.

We need to single-quote string literals when they are arguments to an Erlang function. If you have a UTF-8 string stored in an Elixir variable, you can convert it to a char list with the binary_to_list/1 function.

Edit 3:

Ironically the latest Elixir Sip titled "HTTP clients" that came out a few hours ago covers my entire question. Check it out anyone coming across this is the future!

like image 414
Willl Avatar asked Nov 20 '13 22:11

Willl


3 Answers

For https requests, there is one additional step - the need for SSL:

> :inets.start
> :ssl.start
> :httpc.request ‘https://elixir-lang.org'

Note the shorter request format.

I am using IEx 1.7.3 (compiled with Erlang/OTP 21).

like image 159
pob Avatar answered Oct 05 '22 21:10

pob


"" in erlang is char list while in elixir is ''("" in elixir is binary)

you can use :httpc.request('http://www.erlang.org') shortly

like image 44
fairjm Avatar answered Oct 05 '22 20:10

fairjm


How is the following?

:inets.start
:httpc.request(:get, {'http://www.erlang.org', []}, [], [])
like image 32
parroty Avatar answered Oct 05 '22 20:10

parroty