View Source HTTPSpec.Request (http_spec v2.3.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.

Creates a request from given options.

Bang version of new/1.

Puts body into request.

Puts a request header name to value.

Puts a request header name to value unless already present.

Puts query into request.

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()
}
@type url() :: String.t()

Functions

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

Builds a method.

Examples

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

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

Builds an URL.

Examples

iex> request = HTTPSpec.Request.new!(
...>   method: :get
...>   scheme: :https,
...>   host: "www.example.com",
...>   port: 443,
...>   path: "/image.png",
...>   query: "size=lg",
...>   fragment: "124,28"
...> )
iex> HTTPSpec.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> HTTPSpec.Request.get_header(request, "cache-control")
["max-age=600", "no-transform"]
iex> request = Request.delete_header(req, "cache-control")
iex> HTTPSpec.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> HTTPSpec.Request.get_header(request, "accept")
["application/json"]
iex> HTTPSpec.Request.get_header(requset, "x-unknown")
[]
@spec new(keyword() | map()) :: {:ok, t()} | {:error, HTTPSpec.ArgumentError.t()}

Creates a request from given options.

The options can be provided as a keyword list or a map.

Examples

HTTPSpec.Request.new(%{
  method: :post,
  scheme: :https,
  host: "www.example.com",
  port: 443,
  path: "/talk",
  headers: [
    {"content-type", "application/x-www-form-urlencoded"},
    {"accept", "text/html"}
  ],
  body: "say=Hi&to=Mom",
  query: "tone=cute"
})

And, an url option is provided for setting scheme, host, port, path and query in a quick way.

HTTPSpec.Request.new(%{
  method: :post,
  url: "https://www.example.com/talk?tone=cute",
  headers: [
    {"content-type", "application/x-www-form-urlencoded"},
    {"accept", "text/html"}
  ],
  body: "say=Hi&to=Mom"
})
@spec new!(keyword() | map()) :: t()

Bang version of new/1.

@spec put_body(t(), body()) :: t()

Puts body into request.

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> HTTPSpec.Request.get_header(request, "accept")
[]
iex> request = Request.put_header(request, "accept", "application/json")
iex> HTTPSpec.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
...>   |> HTTPSpec.Request.put_new_header("accept", "application/json")
...>   |> HTTPSpec.Request.put_new_header("accept", "text/html")
iex> HTTPSpec.Request.get_header(request, "accept")
["application/json"]
Link to this function

put_new_lazy_header(request, name, fun)

View Source
@spec put_new_lazy_header(t(), binary(), (-> binary()) | (t() -> binary())) :: t()

Lazy version of put_new_header/3.

Link to this function

put_query(request, query)

View Source
@spec put_query(t(), query()) :: t()

Puts query into request.