Peppermint
Simple Elixir HTTP client build on Mint. It supports both HTTP 1 and HTTP 2 requests.
Peppermint aims to provide a simple interface build on the modern low-level Mint library. It provides a process-less and pool-less architecture.
Currently peppermint requires elixir ~> 1.10
Installation
Add to your mix.exs
and run mix deps.get
:
def deps do
[
{:peppermint, "~> 0.1.0"},
{:castore, "~> 0.1.0"}
]
end
Usage
Examples
GET
{:ok, %{status: 200, headers: headers, body: body}} =
Peppermint.get("http://httpbin.org/get?foo=bar")
GET with params (sent as query in the path)
{:ok, %{status: 200, headers: headers, body: body}} =
Peppermint.get("http://httpbin.org/get", params: %{foo: "bar"})
POST with params
{:ok, %{status: 200, headers: headers, body: body}} =
Peppermint.post("http://httpbin.org/post", params: %{foo: "bar"})
POST JSON
{:ok, %{status: 200, headers: headers, body: body}} =
Peppermint.post("http://httpbin.org/post",
headers: [{"Content-Type", "application/json"}],
body: Jason.encode!(%{foo: "bar"})
)
Other methods
put
, patch
, delete
, head
, options
and trace
Timeouts
transport_options
: See mint docs - Thetimeout
here specifies the connect timeout (defaults to30_000
)receive_timeout
- Trigger timeout if no data received for x ms (defaults to5_000
)
Peppermint.get("http://httpbin.org/get",
receive_timeout: 1_000,
transport_options: [timeout: 5_000]
)
Reuse connection
You can reuse your connection if need to do multiple requests to the same host. For more advanced usecases you should use Mint directly.
{:ok, conn} = Peppermint.connect("http://httpbin.org", [])
{:ok, conn, response1} = Peppermint.request(conn, :get, "/get?foo=bar", [])
{:ok, conn, response2} = Peppermint.request(conn, :post, "/post", params: %{foo: "bar"})
{:ok, _conn} = Peppermint.close(conn)