View Source HTTPSpec.Request (http_spec v3.0.0)

A struct for describing HTTP requests.

Summary

Functions

Builds a method.

Builds an URL.

Creates a request from given options.

Bang version of new/1.

Puts body into request.

Puts query into request.

Types

@type body() :: iodata() | nil
@type fragment() :: String.t() | nil
@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: HTTPSpec.Header.headers(),
  host: host(),
  method: method(),
  path: path(),
  port: :inet.port_number(),
  query: query(),
  scheme: scheme(),
  trailers: HTTPSpec.Trailer.trailers()
}
@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"
@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_query(request, query)

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

Puts query into request.