HTTP client for Elixir without dependencies


Keywords
client, elixir, http, http-client, httpc, no-dependencies, simplehttp
License
Other

Documentation

SimpleHttp Travis Hex.pm Hex.pm Hex.pm Coverage Status

Unlike other projects / libraries, SimpleHttp doesn't have other dependencies

SimpleHttp myfreeweb/httpotion edgurgel/httpoison
	   
$ mix app.tree      

simplehttp ├── elixir └── logger └── elixir


$ mix app.tree

httpotion ├── elixir ├── ssl │ ├── crypto │ └── public_key │ ├── asn1 │ └── crypto └── ibrowse


$ mix app.tree

httpoison ├── elixir └── hackney ├── crypto ├── asn1 ├── public_key │ ├── asn1 │ └── crypto ├── ssl │ ├── crypto │ └── public_key ├── idna ├── mimerl ├── certifi ├── ssl_verify_fun │ └── ssl └── metrics

Hex Installation

  1. Add simplehttp to your list of dependencies in mix.exs:
```elixir
def deps do
  [{:simplehttp, "~> 0.5.1"}]
end
```

Usage

Note:You can load SimpleHttp into the Elixir REPL by executing this command from the root of your project:

$ iex -S mix

Simple GET Request

    {:ok, response} = SimpleHttp.get "http://jsonplaceholder.typicode.com/posts/1"

    IO.inspect response.status 
    ~s"""
        {'HTTP/1.1', 200, 'OK'}
    """

    IO.inspect response.headers
    ~s"""
   		[{'cache-control', 'public, max-age=14400'}, {'connection', 'keep-alive'},
		 {'content-length', '292'}, {'content-type', 'application/json; charset=utf-8'},
		 {'expires', 'Wed, 31 Aug 2016 22:15:00 GMT'},
		 {'set-cookie',
		  '__cfduid=d42; expires=Thu, 31-Aug-17 18:15:00 GMT; path=/; domain=.typicode.com; HttpOnly'},
		 {'cf-ray', '2db27722e62f0479-FRA'}] 
    """   

    IO.puts response.body
	~s"""
		{
		  "userId": 1,
		  "id": 1,
		  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
		  "body": "bla bla"
		}
	"""

GET Request with query params

    {:ok, response} = SimpleHttp.get "http://jsonplaceholder.typicode.com/posts/1", [
      query_params: [
        postId: 1
      ]
    ]

    IO.inspect response.status 
    IO.inspect response.headers
    IO.puts response.body

POST with JSON

    {:ok, response} = SimpleHttp.post "http://jsonplaceholder.typicode.com/posts", [
      body: "{\"name\":\"foo.example.com\"}",
      headers: %{
        "Content-Type" => "application/x-www-form-urlencoded",
        "Authorization" => "Bearer hash",
        "X-Customer" => "123"
      }
      timeout: 1000,
      connect_timeout: 1000
    ]

    IO.inspect response.status 
    IO.inspect response.headers
    IO.puts response.body

POST with params

    {:ok, response} = SimpleHttp.post "http://jsonplaceholder.typicode.com/posts", [
      params: [
        title: "title is present here",
        message: "hello world!"
      ],
      headers: %{
        "Content-Type" => "application/x-www-form-urlencoded",
        "Authorization" => "Bearer hash",
        "X-Customer" => "123"
      }
      timeout: 1000,
      connect_timeout: 1000
    ]

    IO.inspect response.status 
    IO.inspect response.headers
    IO.puts response.body