# Basic code to use mint (http client)

**URL:** <https://forum.elixirforum.com/t/basic-code-to-use-mint-http-client/23701>\
**Category:** Questions\
**Tags:** http\_client, http\
**Created:** [July 4, 2019, 7:38pm UTC](https://forum.elixirforum.com/t/basic-code-to-use-mint-http-client/23701 "2019-07-04T19:38:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [July 4, 2019, 7:38pm UTC](https://forum.elixirforum.com/t/basic-code-to-use-mint-http-client/23701/1 "2019-07-04T19:38:08Z")

</div>

Hello,

I am trying to use mint to make some http requests with Elixir, and reading the docs I wrote the following code.

It works fine (for basic GET requests and everything is hardcoded, it’s normal, just a test) but it seems a bit to much to use a library.

Questions at the end of the post.

```elixir
defmodule HTTP do
  @empty_state %{data: [], done: false}

  def run do
    with {:ok, conn} <- Mint.HTTP.connect(:http, "httpbin.org", 80),
         {:ok, conn, _ref} <- Mint.HTTP.request(conn, "GET", "/", [], nil) do
      handle_response(conn, @empty_state)
    end
  end

  defp handle_response(conn, state) do
    receive do
      message ->
        case Mint.HTTP.stream(conn, message) do
          {:ok, conn, responses} ->
            case Enum.reduce(responses, state, &handle_res/2) do
              # Loop ends here
              %{done: true} = state -> {:ok, state}
              %{done: false} = state -> handle_response(conn, state)
            end

          {:error, _, reason, _} ->
            {:error, reason}

          :unknown ->
            exit({:unexpected, message})
        end
    end
  end

  defp handle_res({:status, _, status}, state),
    do: Map.put(state, :status, status)

  defp handle_res({:headers, _, headers}, state),
    do: Map.put(state, :headers, headers)

  defp handle_res({:data, _, data}, state),
    do: Map.update!(state, :data, fn acc -> [data | acc] end)

  defp handle_res({:done, _}, state) do
    Map.update!(state, :data, fn acc ->
      acc
      |> :lists.reverse()
      |> Enum.join("")
    end)
    |> Map.put(:done, true)
  end
end

Task.async(fn -> HTTP.run() end)
|> Task.await()
|> case do
  {:ok, state} ->
    state
    |> Map.get(:data)
    |> IO.puts()

  other ->
    IO.inspect(other, pretty: true)
end

```

A quick note : I intend to use this from a GenServer, and I have no concerns about performance for this project, so to avoid handling messages, I will spawn a `Task` to handle the request, so I do not care about the request reference or `:unknown` messages. Also I know there are other responses possibles.

But again I do not expect other messages.

**Questions**

- Is my code `:ok` ? I’ll take any remarks.
- Am I expected to do that to use this library ?
- Do you think `mint` should provide a helper like this for basic single requests ?
- If I got it right, `connect/3` is separated from `request/5` for connection reuse. Can I do multiple requests with the same `conn` ? And if yes, should I use the conn from `connect()` or the last updated one ?

Thank you !

---

<div class="post-metadata">

**Author:** ![jola](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jola/32/18010_2.png) [@jola](https://forum.elixirforum.com/u/jola)\
**Post date:** [July 4, 2019, 7:57pm UTC](https://forum.elixirforum.com/t/basic-code-to-use-mint-http-client/23701/2 "2019-07-04T19:57:18Z")

</div>

If you’re just looking to make some requests, look at HTTPoison (based on hackney) or Mojito (based on Mint).

Mint is a low level library which is pretty high effort if you’re not looking specifically for the features it offers. It explicitly doesn’t provide helpers to do requests, look at Mojito for that.

---

<div class="post-metadata">

**Author:** ![outlog](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@outlog](https://forum.elixirforum.com/u/outlog)\
**Post date:** [July 4, 2019, 8:39pm UTC](https://forum.elixirforum.com/t/basic-code-to-use-mint-http-client/23701/3 "2019-07-04T20:39:40Z")

</div>

what @jola said - just wanted to add the tesla [GitHub - elixir-tesla/tesla: The flexible HTTP client library for Elixir, with support for middleware and multiple adapters. · GitHub](https://github.com/teamon/tesla) library which is pretty nice for creating an abstraction/module for your different requests etc..

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [July 4, 2019, 9:06pm UTC](https://forum.elixirforum.com/t/basic-code-to-use-mint-http-client/23701/4 "2019-07-04T21:06:39Z")

</div>

I used to use hackney but those libraries seem great too 🙂

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [July 4, 2019, 10:50pm UTC](https://forum.elixirforum.com/t/basic-code-to-use-mint-http-client/23701/5 "2019-07-04T22:50:33Z")

</div>

I highly recommend Tesla. It supports changing of the underlying HTTP client while providing a very sensible and convenient thin abstraction layer above them.
