2

I'm new to Elixir. I was hoping it would be similar to Python or R where modules can be downloaded and kept up-to-date without too much effort.

I have already created a mix project to use HTTPoison but I really just want to try out HTTPoison from the interpreter.

$ iex
Erlang/OTP 22 [erts-10.4.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.9.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> import HTTPoison
** (CompileError) iex:1: module HTTPoison is not loaded and could not be found

iex(1)>
AG1
  • 6,648
  • 8
  • 40
  • 57
  • 3
    You will need to create a mix project and add `:httpoison` to your list of dependencies. Then you can run `mix deps.get` in order to actually retrieve your dependencies. Then in the projects directory, you can run `iex -S mix` in order to open an `iex` session with your projects settings. You can technically download `httpoison`, compile it, and add the directory the beam files are in into an option you pass into iex (https://stackoverflow.com/questions/36065774/how-do-i-load-an-elixir-library-into-iex-without-adding-it-to-a-projects-mix-ex), but that is probably not worth it. – Justin Wood Jul 29 '19 at 02:27
  • 2
    Possible duplicate of [How do I load an Elixir library into iex without adding it to a project's mix.exs deps?](https://stackoverflow.com/questions/36065774/how-do-i-load-an-elixir-library-into-iex-without-adding-it-to-a-projects-mix-ex) – Adam Millerchip Jul 29 '19 at 04:48

1 Answers1

8

Here are the steps that Justin Wood described. mix is elixir's package manager.

If you want to use HTTPoison in iex, here is what you should do:

1)

$ mix new myproj

* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/myproj.ex
* creating test
* creating test/test_helper.exs
* creating test/myproj_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd myproj
    mix test

Run "mix help" for more commands.

2)

$ cd myproj

3) Go to hex.pm and look up the latest version of httpoison:

enter image description here

At the top right, under mix.exs, copy the tuple {:httpoison, "~> 1.5"}.

4) In your myproj/mix.exs file add the tuple to the deps:

enter image description here

5) Download the dependencies:

 .../myproj$ mix deps.get

Resolving Hex dependencies...
Dependency resolution completed:
New:
  certifi 2.5.1
  hackney 1.15.1
  httpoison 1.5.1
  idna 6.0.0
  metrics 1.0.1
  mimerl 1.2.0
  parse_trans 3.3.0
  ssl_verify_fun 1.1.4
  unicode_util_compat 0.4.1
* Getting httpoison (Hex package)
* Getting hackney (Hex package)
* Getting certifi (Hex package)
* Getting idna (Hex package)
* Getting metrics (Hex package)
* Getting mimerl (Hex package)
* Getting ssl_verify_fun (Hex package)
* Getting unicode_util_compat (Hex package)
* Getting parse_trans (Hex package)

6) Launch iex:

.../myproj$ iex -S mix

Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Compiling idna
==> ssl_verify_fun
Compiling 7 files (.erl)
Generated ssl_verify_fun app
===> Compiling certifi
===> Compiling hackney
==> httpoison
Compiling 3 files (.ex)
Generated httpoison app
==> myproj
Compiling 1 file (.ex)
Generated myproj app
Interactive Elixir (1.8.2) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> 

7) Start httpoison and issue some requests:

iex(1)> HTTPoison.start
{:ok, []}

iex(2)> HTTPoison.get! "http://google.com"                 
%HTTPoison.Response{
  body: "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.com/\">here</A>.\r\n</BODY></HTML>\r\n",
  headers: [
    {"Location", "http://www.google.com/"},
    {"Content-Type", "text/html; charset=UTF-8"},
    {"Date", "Mon, 29 Jul 2019 03:56:22 GMT"},
    {"Expires", "Wed, 28 Aug 2019 03:56:22 GMT"},
    {"Cache-Control", "public, max-age=2592000"},
    {"Server", "gws"},
    {"Content-Length", "219"},
    {"X-XSS-Protection", "0"},
    {"X-Frame-Options", "SAMEORIGIN"}
  ],
  request: %HTTPoison.Request{
    body: "",
    headers: [],
    method: :get,
    options: [],
    params: %{},
    url: "http://google.com"
  },
  request_url: "http://google.com",
  status_code: 301
}
7stud
  • 46,922
  • 14
  • 101
  • 127
  • 3
    If you are just going to take me comment and turn it into an answer, shouldn't you mark this as a community wiki? – Justin Wood Jul 29 '19 at 13:54