Skip to content


Subversion checkout URL

You can clone with
Download ZIP
The HTTP client for Elixir
branch: master

Merge pull request #59 from strand/patch-1

latest commit 017c1cba75
@myfreeweb authored

HTTPotion version downloads Build Status WTFPL

HTTP client for Elixir, based on ibrowse. Continues the HTTPun tradition of HTTParty, HTTPretty, HTTParrot and HTTPie.


Add HTTPotion and ibrowse to your project's dependencies in mix.exs:

  defp deps do
      {:ibrowse, github: "cmullaparthi/ibrowse", tag: "v4.1.1"},
      {:httpotion, "~> 2.1.0"}

  def application do
    [applications: [:httpotion]]
    # Application dependency auto-starts it, otherwise: HTTPotion.start

And fetch your project's dependencies:

$ mix deps.get


Some basic examples:

iex> response = HTTPotion.get ""
%HTTPotion.Response{body: "...", headers: [Connection: "keep-alive", ...], status_code: 200}

iex> HTTPotion.Response.success?(response)

iex> response = "", [body: "hello=world", headers: ["User-Agent": "My App"]]
%HTTPotion.Response{body: "...", headers: [Connection: "keep-alive", ...], status_code: 200}

iex> response = HTTPotion.request :propfind, "", [body: "I have no idea what I'm doing"]
%HTTPotion.Response{body: "...", headers: [Connection: "keep-alive", ...], status_code: 405}

iex> response = HTTPotion.get "", [basic_auth: {"foo", "bar"}]
%HTTPotion.Response{body: "...", headers: ["Access-Control-Allow-Credentials": "true", ...], status_code: 200}

iex> HTTPotion.get "http://localhost:1"
** (HTTPotion.HTTPError) econnrefused

The Response is a struct – you access its fields like this: response.body.

HTTPError is an exception that happens when the request fails.

Note: the API changed in 2.0.0, body and headers are options now!

Metaprogramming magic

You can extend HTTPotion.Base to make cool API clients or something (this example uses jsx for JSON):

defmodule GitHub do
  use HTTPotion.Base

  def process_url(url) do
    "" <> url

  def process_request_headers(headers) do
    Dict.put headers, :"User-Agent", "github-potion"

  def process_response_body(body) do
    body |> to_string |> :jsx.decode
    |> fn ({k, v}) -> { String.to_atom(k), v } end
    |> :orddict.from_list
iex> GitHub.get("users/myfreeweb").body[:public_repos]

Read the source to see all the hooks. It's not intimidating at all, pretty easy to read actually :-)

Asynchronous requests

Hey, we're on the Erlang VM, right? Every serious OTP app probably makes a lot of these. It's easy to do in HTTPotion.

iex> HTTPotion.get "", [stream_to: self]
%HTTPotion.AsyncResponse{id: {1372,8757,656584}}

iex> flush
%HTTPotion.AsyncHeaders{id: {1372,8757,656584}, status_code: 200, headers: ["Transfer-Encoding": "chunked", ...]}
%HTTPotion.AsyncChunk{id: {1372,8757,656584}, chunk: "<!DOCTYPE html>\n..."}
%HTTPotion.AsyncEnd{id: {1372,8757,656584}}

Direct access to ibrowse workers

ibrowse allows you to use its separate worker processes directly. We expose this functionality through the direct option.

Don't forget that you have to pass the URL to the worker process, which means the worker only communicates with one server (domain!)

iex> {:ok, worker_pid} = HTTPotion.spawn_worker_process("")

iex> HTTPotion.get "", [direct: worker_pid]
%HTTPotion.Response{body: "...", headers: ["Connection": "close", ...], status_code: 200}

You can even combine it with async!

iex> {:ok, worker_pid} = HTTPotion.spawn_worker_process("")

iex> "", [direct: worker_pid, stream_to: self, headers: ["User-Agent": "hello it's me"]]
%HTTPotion.AsyncResponse{id: {1372,8757,656584}}


Please feel free to submit pull requests! Bugfixes and simple non-breaking improvements will be accepted without any questions :-)

By participating in this project you agree to follow the Contributor Code of Conduct.


Copyright © 2013-2015 HTTPotion Contributors.

This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details.

Something went wrong with that request. Please try again.