httpoison 0.6.2

Yet Another HTTP client for Elixir powered by hackney

Homepage: https://hex.pm/packages/httpoison

Platform: Hex

Language: Elixir

License: MIT

Repository: https://github.com/edgurgel/httpoison

View on registry: https://hex.pm/packages/httpoison/

Documentation: http://hexdocs.pm/httpoison/0.6.2


HTTPoison Build Status Hex pm

HTTP client for Elixir, based on HTTPotion (documentation).

But... why something so similar to HTTPotion?

HTTPoison uses hackney to execute HTTP requests instead of ibrowse. I like hackney :thumbsup:

Using hackney we work only with binaries instead of string lists.

Installation

First, add HTTPoison to your mix.exs dependencies:

def deps do
 [{:httpoison, "~> 0.6"}]
end

and run $ mix deps.get. Now, list the :httpoison application as your application dependency:

def application do
 [applications: [:httpoison]]
end

Usage

iex> HTTPoison.start
iex> HTTPoison.get! "http://httparrot.herokuapp.com/get"
%HTTPoison.Response{
 body: "{\n \"args\": {},\n \"headers\": {} ...",
 headers: %{"connection" => "keep-alive", "content-length" => "517", ...},
 status_code: 200
}
iex> HTTPoison.get! "http://localhost:1"
** (HTTPoison.Error) :econnrefused
iex> HTTPoison.get "http://localhost:1"
{:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}

You can also easily pattern match on the HTTPoison.Response struct:

case HTTPoison.get(url) do
 {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
 IO.puts body
 {:ok, %HTTPoison.Response{status_code: 404}} ->
 IO.puts "Not found :("
 {:error, %HTTPoison.Error{reason: reason}} ->
 IO.inspect reason
end

Wrapping HTTPoison.Base

You can also use the HTTPoison.Base module in your modules in order to make cool API clients or something. The following example wraps HTTPoison.Base in order to build a client for the GitHub API (Poison is used for JSON decoding):

defmodule GitHub do
 use HTTPoison.Base
 def process_url(url) do
 "https://api.github.com" <> url
 end
 def process_response_body(body) do
 body
 |> Poison.decode!
 |> Enum.map(fn({k, v}) -> {String.to_atom(k), v} end)
 end
end
iex> GitHub.start
iex> GitHub.get!("/users/myfreeweb").body[:public_repos]
37

It's possible to extend the functions listed below:

defp process_request_body(body), do: body
defp process_response_body(body), do: body
defp process_request_headers(headers) when is_map(headers) do
 Enum.into(headers, [])
end
defp process_request_headers(headers), do: headers
defp process_response_chunk(chunk), do: chunk
defp process_headers(headers), do: Enum.into(headers, %{})
defp process_status_code(status_code), do: status_code

Async requests

HTTPoison now comes with async requests!

iex> HTTPoison.get! "http://floatboth.com", %{}, stream_to: self
%HTTPoison.AsyncResponse{id: #Reference<0.0.0.1654>}
iex> flush
%HTTPoison.AsyncStatus{code: 200, id: #Reference<0.0.0.1654>}
%HTTPoison.AsyncHeaders{headers: %{"Connection" => "keep-alive", ...}, id: #Reference<0.0.0.1654>}
%HTTPoison.AsyncChunk{chunk: "<!DOCTYPE html>...", id: #Reference<0.0.0.1654>}
%HTTPoison.AsyncEnd{id: #Reference<0.0.0.1654>}
:ok

You can see more usage examples in the test files (located in the test/) directory.

License

Copyright © 2013-2014 Eduardo Gurgel <eduardo@gurgel.me>
This work is free. You can redistribute it and/or modify it under the
terms of the MIT License. See the LICENSE file for more details.
веселые картинки развлекательные гифки интресные факты смешные видео смешные истории из соцсетей

GitHub Repository

edgurgel/httpoison edgurgel/httpoison

Yet Another HTTP client for Elixir powered by hackney

https://hex.pm/packages/httpoison

Language: Elixir

Created: September 11, 2013 07:21

Last updated: March 09, 2015 01:06

Last pushed: March 04, 2015 05:46

Size: 878 KB

Stars: 101

Forks: 25

Watchers: 7

Open issues: 4

Top Contributors

Eduardo Gurgel Jamie Winsor d0rc Andrea Leopardi Igor Kapkov Devin Torres Eger Andreas Josh Adams Kurt Williams Glenn Rempe Julien Boyer niku parroty Takayuki Matsubara Blake Williams

Releases

  • 0.6.2 - February 17, 2015 09:23
  • 0.6.1 - February 11, 2015 08:45
  • 0.6.0 - January 26, 2015 08:50
  • 0.5.0 - October 14, 2014 08:33
  • 0.4.3 - October 09, 2014 22:25
  • 0.4.2 - September 03, 2014 09:45
  • 0.4.1 - August 21, 2014 12:39
  • 0.4.0 - August 17, 2014 10:19
  • 0.3.2 - August 04, 2014 21:04
  • 0.3.1 - August 02, 2014 00:10
See all 12 releases

Related Projects

bertex 1.2.0
Elixir BERT encoder/decoder
Hex - Elixir - MIT - Published 7 months ago - 12 stars
cloudi_service_http_client 1.4.0
Erlang/Elixir Cloud Framework HTTP client Service
Hex - Erlang - BSD-3-Clause - Updated 2 months ago
consul 1.0.3
An Elixir client for Consul's HTTP API
Hex - Elixir - MIT - Updated about 1 month ago - 3 stars
exsync 0.0.1
Yet another Elixir reloader.
Hex - Elixir - BSD-3-Clause-Clear - Published 5 months ago
httparrot 0.3.4
HTTP Request & Response Server. An incomplete clone of http://httpbin.org
Hex - Elixir - MIT - Updated about 2 months ago - 18 stars
веселые картинки развлекательные гифки интресные факты смешные видео смешные истории из соцсетей