raxx v0.12.0 Raxx

Tooling to work with HTTP.

Several data structures are defined to model parts of the communication between client and server.

This module contains functions to create and manipulate these structures.

See Raxx.Server for implementing a web application.

Link to this section Summary

Functions

Does the message struct contain all the data to be sent

Add a complete body to a message

Set the value of a header field

Add a query value to a request

Link to this section Functions

Does the message struct contain all the data to be sent.

Examples

iex> request(:GET, "/")
...> |> complete?()
true

iex> response(:ok)
...> |> set_body("Hello, World!")
...> |> complete?()
true

iex> response(:ok)
...> |> set_body(true)
...> |> complete?()
false
Link to this function fragment(data, end_stream \\ false)

Construct a Raxx.Fragment

A fragment encapsulates a section of message content that has been generated. If a stream has no trailers then the final fragment should mark the stream as ended.

Examples

iex> fragment("Hi").data
"Hi"

iex> fragment("Hi", true).end_stream
true

iex> fragment("Hi").end_stream
false
Link to this function request(method, url)

Construct a Raxx.Request.

An HTTP request must have a method and path.

If the location argument is a relative path the scheme and authority values will be unset. When these values can be inferred from the location they will be set.

The method must be an atom for one of the HTTP methods

[:GET, :POST, :PUT, :PATCH, :DELETE, :HEAD, :OPTIONS]

The request will have no body or headers. These can be added with set_header/3 and set_body/2.

Examples

iex> request(:HEAD, "/").method
:HEAD

iex> request(:GET, "/").path
[]

iex> request(:GET, "/foo/bar").path
["foo", "bar"]

iex> request(:GET, "https:///").scheme
:https

iex> request(:GET, "https://example.com").authority
"example.com"

iex> request(:GET, "/?foo=bar").query
%{"foo" => "bar"}

iex> request(:GET, "/").headers
[]

iex> request(:GET, "/").body
false
Link to this function response(status_code)

Construct a Raxx.Response.

The responses HTTP status code can be provided as an integer, or will be translated from a known atom.

The response will have no body or headers. These can be added with set_header/3 and set_body/2.

Examples

iex> response(200).status
200

iex> response(:no_content).status
204

iex> response(200).headers
[]

iex> response(200).body
false
Link to this function set_body(message, body)

Add a complete body to a message.

Examples

iex> request(:GET, "/")
...> |> set_body("Hello")
...> |> Map.get(:body)
"Hello"
Link to this function set_header(message, name, value)

Set the value of a header field.

Examples

iex> request(:GET, "/")
...> |> set_header("referer", "example.com")
...> |> set_header("accept", "text/html")
...> |> Map.get(:headers)
[{"referer", "example.com"}, {"accept", "text/html"}]
Link to this function set_query(request, query)

Add a query value to a request

Examples

iex> request(:GET, "/")
...> |> set_query(%{"value" => "1"})
...> |> Map.get(:query)
%{"value" => "1"}
Link to this function trailer(headers)

Construct a Raxx.Trailer

Examples

iex> trailer([{"digest", "opaque-data"}]).headers
[{"digest", "opaque-data"}]