HTTPipe v0.9.0 HTTPipe

Provides helper functions that allow for quick, one-off HTTP requests. To build composable requests, see the HTTPipe.Conn module.

The functions in this module are designed to provide a (somewhat) parity interface with HTTPoison and HTTPotion. Each major HTTP method has its own function that you can call. For example, to perform an HTTP GET request, it is as simple as:

{:ok, conn} = HTTPipe.get("https://httpbin.org/get")

See the note about configuring an adapter in the README.

The conn that is captured in the above statement is an HTTPipe.Conn struct. All the functions in the module have a return signature of {:ok, Conn.t} | {:error, Conn.t} except for trailing bang (e.g., get!) functions which have a return signature of Conn.t | no_return–anything that would have resulted in an {:error, Conn.t} response before will instead raise the appropriate exception.

If you are coming from HTTPoison or HTTPotion, it is important to note that this library does not return a Response or ErrorResponse struct. This library returns the Conn.t struct which encapsulates the entire transaction. If an error occured, the Conn.t struct returned will include the exception under the :error key.

For a slightly more complicated example, let’s try handling the response. httpbin returns its results as JSON, so for this example, we’ll assume you have the Poison library. The /get path will return the IP address of the client under the "origin" key.

with {:ok, %{response: %{body: body}}} <- HTTPipe.get("https://httpbin.org/get"),
     {:ok, decoded_body} <- Poison.decode(body),
  do: Map.get(decoded_body, "origin")

Headers

Each of the functions in this module allow for custom headers to be passed as a map of the form %{String.t => String.t}. If the function accepts a body, the headers can be passed as the third parameter; otherwise the headers can be passed as the second parameter. By default, an empty map will be passed. For example, let’s set the "Accept" header:

HTTPipe.get("https://httpbin.org/get", %{"Accept" => "application/xml"})

For more information, see the type documentation for HTTPipe.Request.headers.

Body

If the function accepts a body, it will be the second parameter to the function. The body can be passed either as nil, a String.t, or a special tuple that will be processed before executing the connection. For example, to send URL encoded form data, you can use the {:form, data} tuple. The data should be a Keyword.t list no more than one level deep.

post_body = {:form, [name: "HTTPipe", language: "Elixir"]}

HTTPipe.post("https://httpbin.org/post", post_body)

Based on the above connection, the server will receive the following body:

name=HTTPipe&language=Elixir

For more information, see the type documentation for HTTPipe.Request.body.

Options

The final parameter of every function in this module is a keyword list of options. The following options are available:

:adapter

You can use this key to modify the adapter used for a specific connection. For example:

httpc_adapter = HTTPipe.Adapters.HTTPC

# Uses :default adapter
{:ok, conn} = HTTPipe.get("https://httpbin.org/get", %{})
# Uses HTTPC Adapter
{:ok, conn} = HTTPipe.get("https://httpbin.org/get", %{}, [adapter: httpc_adapter])

:adapter_options

The :adapter_options key will be passed directly to the adapter. The adapter you choose should document the options that can be passed in here.

:params

This is a keyword list of query parameters to be appended to the URL you specified. The list should be no deeper than a single key-value pair. This option is primarily to provide compatiability with HTTPoison/HTTPotion. For example:

params = [q: "HTTPipe adapter", language: "elixir"]

{:ok, conn} = HTTPipe.get("https://httpbin.org/get", %{}, [params: params])

The server will receive the following URL request:

https://httpbin.org/get?q=HTTPipe+adapter&language=elixir

Further Testing

If you want to try out the functions more, you can use RequestBin to see how these requests are made to the server. RequestBin gives you a unique, disposable URL which you can send requests to. The requests will then be displayed on the dashboard. For example, let’s try posting to a RequestBin:

url = "http://requestb.in/15mo0ew1"
post_body = {:form, [test_param: "test value"]}
headers = %{"content-type" => "application/x-www-form-urlencoded"}

HTTPipe.post(url, post_body, headers)

Summary

Functions

Performs an HTTP DELETE request on the given resource

Identical to delete/3 but raises an error if the request could not be completed successfully

Performs an HTTP GET request on the given resource

Identical to get/3 but raises an error if the request could not be completed successfully

Performs an HTTP HEAD request on the given resource

Identical to head/3 but raises an error if the request could not be completed successfully

Performs an HTTP OPTIONS request on the given resource

Identical to options/3 but raises an error if the request could not be completed successfully

Performs an HTTP PATCH request on the given resource

Identical to patch/4 but raises an error if the request could not be completed successfully

Perforns an HTTP POST request on the given resource

Identical to post/4 but raises an error if the request could not be completed succesfully

Performs an HTTP PUT request on the given resource

Identical to put/4 but raises an error if the request could not be completed succesfully

Performs a generic HTTP request using the given method on the requested resource. This function is used by all other functions in this module to complete their requests

Identical to request/5 but will raise an error if the request could not be completed succesfully

Functions

delete(url, headers \\ %{}, options \\ [])

Performs an HTTP DELETE request on the given resource.

delete!(url, headers \\ %{}, options \\ [])

Identical to delete/3 but raises an error if the request could not be completed successfully.

get(url, headers \\ %{}, options \\ [])

Performs an HTTP GET request on the given resource.

get!(url, headers \\ %{}, options \\ [])

Identical to get/3 but raises an error if the request could not be completed successfully.

head(url, headers \\ %{}, options \\ [])

Performs an HTTP HEAD request on the given resource.

head!(url, headers \\ %{}, options \\ [])

Identical to head/3 but raises an error if the request could not be completed successfully.

options(url, headers \\ %{}, options \\ [])

Performs an HTTP OPTIONS request on the given resource.

Compatability Note

In order to maintain compatability with libraries such as HTTPoison and HTTPotion, the options/3 and options!/3 functions do not accept body fields, even though RFC 7231 allows for OPTIONS requests to have a body.

If you need to make an OPTIONS request with a body, please use request/5 or request!/5 as appropriate instead.

options!(url, headers \\ %{}, options \\ [])

Identical to options/3 but raises an error if the request could not be completed successfully.

patch(url, body, headers \\ %{}, options \\ [])

Performs an HTTP PATCH request on the given resource.

patch!(url, body, headers \\ %{}, options \\ [])

Identical to patch/4 but raises an error if the request could not be completed successfully.

post(url, body, headers \\ %{}, options \\ [])

Perforns an HTTP POST request on the given resource.

post!(url, body, headers \\ %{}, options \\ [])

Identical to post/4 but raises an error if the request could not be completed succesfully.

put(url, body, headers \\ %{}, options \\ [])

Performs an HTTP PUT request on the given resource.

put!(url, body, headers \\ %{}, options \\ [])

Identical to put/4 but raises an error if the request could not be completed succesfully.

request(method, url, body, headers \\ %{}, options)

Performs a generic HTTP request using the given method on the requested resource. This function is used by all other functions in this module to complete their requests.

request!(method, url, body, headers \\ %{}, options)

Identical to request/5 but will raise an error if the request could not be completed succesfully.