https://github.com/alexandrubagu/simplehttp
HTTP client for Elixir without dependencies
https://github.com/alexandrubagu/simplehttp
client elixir http http-client httpc no-dependencies simplehttp
Last synced: 21 days ago
JSON representation
HTTP client for Elixir without dependencies
- Host: GitHub
- URL: https://github.com/alexandrubagu/simplehttp
- Owner: alexandrubagu
- License: mit
- Created: 2016-08-31T12:14:25.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-09-26T04:06:41.000Z (over 2 years ago)
- Last Synced: 2026-02-14T18:45:25.413Z (28 days ago)
- Topics: client, elixir, http, http-client, httpc, no-dependencies, simplehttp
- Language: Elixir
- Homepage: https://hex.pm/packages/simplehttp
- Size: 75.2 KB
- Stars: 17
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SimpleHttp [](https://travis-ci.org/alexandrubagu/simplehttp) [](https://hex.pm/packages/simplehttp) [](https://hex.pm/packages/simplehttp) [](https://hex.pm/packages/simplehttp) [](https://coveralls.io/github/alexandrubagu/simplehttp?branch=master)
## 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`:
```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:
```elixir
$ iex -S mix
```
Simple GET Request
```elixir
{: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:
```elixir
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
```elixir
{:ok, response} = SimpleHttp.get "http://jsonplaceholder.typicode.com/posts/1", [
query_params: [
postId: 1
]
]
```
Download a file using a GET request
```elixir
{: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
```elixir
{: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
```elixir
{: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
```elixir
{: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
```