Skip to content

Latest commit

 

History

95 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
	    
$ 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:
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 
{:ok,
%SimpleHttp.Response{
  body: "{\n  \"userId\": 1,\n  \"id\": 1,\n  \"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",\n  \"body\": \"quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto\"\n}",
  headers: [
    {'cache-control', 'public, max-age=14400'},
    {'connection', 'keep-alive'},
    {'date', 'Mon, 22 Oct 2018 07:02:48 GMT'},
    {'pragma', 'no-cache'},
    {'via', '1.1 vegur'},
    {'etag', 'W/"124-yiKdLzqO5gfBrJFrcdJ8Yq0LGnU"'},
    {'server', 'cloudflare'},
    {'vary', 'Origin, Accept-Encoding'},
    {'content-length', '292'},
    {'content-type', 'application/json; charset=utf-8'},
    {'expires', 'Mon, 22 Oct 2018 11:02:48 GMT'},
    {'set-cookie',
      '__cfduid=de34235eb1c3436a238889924c15be9671540191768; expires=Tue, 22-Oct-19 07:02:48 GMT; path=/; domain=.typicode.com; HttpOnly'},
    {'x-powered-by', 'Express'},
    {'access-control-allow-credentials', 'true'},
    {'x-content-type-options', 'nosniff'},
    {'cf-cache-status', 'HIT'},
    {'cf-ray', '46da19b6d5f87ea0-BUD'}
  ],
  status: 200
}}

Use headers_format: :binary option to return headers as strings:

SimpleHttp.get "http://jsonplaceholder.typicode.com/posts/1", headers_format: :binary |> IO.inspect

{:ok,
%SimpleHttp.Response{
  body: "{ ... }",
  headers: [
    {"cache-control", "public, max-age=14400"},
    {"connection", "keep-alive"},
    {"date", "Mon, 22 Oct 2018 07:02:48 GMT"},
    ...
  ],
  status: 200
}}

GET Request with query params

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

Download a file using a GET request

{:ok, %Response{body: :saved_to_file}} = SimpleHttp.get "https://jsonplaceholder.typicode.com/posts", [
  ssl: [verify: :verify_none], headers: %{"User-Agent" => "Mozilla"},
  stream: "/tmp/posts.xml",
  timeout: 5000
]

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
]

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
]

Run a request in a custom HTTP profile with custom options

{:ok, response} = SimpleHttp.get "https://jsonplaceholder.typicode.com/posts", [
  ssl: [verify: :verify_none], headers: %{"User-Agent" => "Mozilla"},
  timeout: 5000,
  profile: :test,
  verbose: :verbose
]
:ok = SimpleHttp.close(response)  # Stop the HTTPC :test profile

About

HTTP client for Elixir without dependencies

Topics

Resources

Stars

17 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages