Skip to content

fidr/peppermint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 pool-less architecture, but it can be used to build your own connection pools easily.

Currently peppermint requires elixir ~> 1.10

Installation

Add to your mix.exs and run mix deps.get:

def deps do
  [
    {:peppermint, "~> 0.3.0"},
    {:castore, "~> 0.1.0"}
  ]
end

Usage

Examples

Fire a one-off request. Connects to the host, executes the request and disconnects.

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 - The timeout here specifies the connect timeout (defaults to 30_000)
  • receive_timeout - Trigger timeout if no data received for x ms (defaults to 5_000)
Peppermint.get("http://httpbin.org/get",
  receive_timeout: 1_000,
  transport_options: [timeout: 5_000]
)

Reuse connection

To reuse a connection, the Peppermint.Connection provides a simple GenServer to handle a connection and simultanious requests over HTTP/2 (multiplexing) or sequentially over HTTP/1:

{:ok, conn} = Peppermint.Connection.open("http://httpbin.org")
{:ok, response} = Peppermint.Connection.request(conn, :get, "/get?foo=bar")
{:ok, response} = Peppermint.Connection.request(conn, :post, "/post", params: %{foo: "bar"})
:ok = Peppermint.Connection.close(conn)

Acknowledgements

  • Check out Mint for more low-level and advanced usecases
  • Check out Mojito if you need connection pooling