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.
Raxx.Request
: metadata sent by a client before sending content.Raxx.Response
: metadata sent by a server before sending content.Raxx.Fragment
: A part of a messages content.Raxx.Trailer
: metadata set by client or server to conclude communication.
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
Construct a Raxx.Fragment
Construct a Raxx.Request
Construct a Raxx.Response
Add a complete body to a message
Set the value of a header field
Add a query value to a request
Construct a Raxx.Trailer
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
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
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
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
Add a complete body to a message.
Examples
iex> request(:GET, "/")
...> |> set_body("Hello")
...> |> Map.get(:body)
"Hello"
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"}]
Add a query value to a request
Examples
iex> request(:GET, "/")
...> |> set_query(%{"value" => "1"})
...> |> Map.get(:query)
%{"value" => "1"}
Construct a Raxx.Trailer
Examples
iex> trailer([{"digest", "opaque-data"}]).headers
[{"digest", "opaque-data"}]