View Source HTTPSpec.Request (http_spec v2.0.0)

A struct for describing HTTP request.

Summary

Functions

Builds a method.

Builds an URL.

Deletes the header given by name.

Returns the values of the header specified by name.

Puts a request header name to value.

Puts a request header name to value unless already present.

Types

@type body() :: iodata() | nil
@type fragment() :: String.t() | nil
@type headers() :: [{header_name :: String.t(), header_value :: String.t()}]
@type host() :: String.t()
@type method() :: atom() | String.t()
@type path() :: String.t()
@type query() :: String.t() | nil
@type scheme() :: :http | :https
@type t() :: %HTTPSpec.Request{
  body: body(),
  fragment: fragment(),
  headers: headers(),
  host: host(),
  method: method(),
  path: path(),
  port: :inet.port_number(),
  query: query(),
  scheme: scheme()
}

Functions

@spec build_method(t()) :: String.t()

Builds a method.

Examples

iex> request = Request.new!([method: :post, ...])
iex> Request.build_method(request)
"POST"

iex> request = Request.new!(method: "POST", ...)
iex> Request.build_method(request)
"POST"
@spec build_url(t()) :: String.t()

Builds an URL.

Examples

iex> request = Request.new!(
...>   method: :get
...>   scheme: :https,
...>   host: "www.example.com",
...>   port: 443,
...>   path: "/image.png",
...>   query: "size=lg",
...>   fragment: "124,28"
...> )
iex> Request.build_url(request)
"https://www.example.com/image.png?size=lg#124,28"
Link to this function

delete_header(request, name)

View Source
@spec delete_header(t(), binary()) :: t()

Deletes the header given by name.

All occurrences of the header are deleted, in case the header is repeated multiple times.

Examples

iex> Request.get_header(request, "cache-control")
["max-age=600", "no-transform"]
iex> request = Request.delete_header(req, "cache-control")
iex> Request.get_header(request, "cache-control")
[]
Link to this function

get_header(request, name)

View Source
@spec get_header(t(), binary()) :: [binary()]

Returns the values of the header specified by name.

Examples

iex> Request.get_header(request, "accept")
["application/json"]
iex> Request.get_header(requset, "x-unknown")
[]
@spec new(keyword() | map()) :: {:ok, t()} | {:error, HTTPSpec.ArgumentError.t()}
@spec new!(keyword() | map()) :: t()
Link to this function

put_header(request, name, value)

View Source
@spec put_header(t(), binary(), binary()) :: t()

Puts a request header name to value.

If the header was previously set, its value is overwritten.

Examples

iex> Request.get_header(request, "accept")
[]
iex> request = Request.put_header(request, "accept", "application/json")
iex> Request.get_header(request, "accept")
["application/json"]
Link to this function

put_new_header(request, name, value)

View Source
@spec put_new_header(t(), binary(), binary()) :: t()

Puts a request header name to value unless already present.

See put_header/3 for more information.

Examples

iex> request =
...>   request
...>   |> Request.put_new_header("accept", "application/json")
...>   |> Request.put_new_header("accept", "text/html")
iex> Request.get_header(request, "accept")
["application/json"]