View Source Req.Response (req v0.4.0)

The response struct.

Fields:

  • :status - the HTTP status code.

  • :headers - the HTTP response headers. The header names must be downcased.

  • :body - the HTTP response body.

  • :trailers - the HTTP response trailers. The trailer names must be downcased.

  • :private - a map reserved for libraries and frameworks to use. Prefix the keys with the name of your project to avoid any future conflicts. Only accepts atom/0 keys.

Summary

Functions

Deletes the header given by name.

Returns the values of the header specified by name.

Gets the value for a specific private key.

Returns the retry-after header delay value or nil if not found.

Builds or updates a response with JSON body.

Returns a new response.

Adds a new response header name if not present, otherwise replaces the previous value of that header with value.

Assigns a private key to value.

Updates private key with the given function.

Types

@type t() :: %Req.Response{
  body: binary() | term(),
  headers: %{required(binary()) => [binary()]},
  private: map(),
  status: non_neg_integer(),
  trailers: %{required(binary()) => [binary()]}
}

Functions

Link to this function

delete_header(response, name)

View Source

Deletes the header given by name.

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

Examples

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

get_header(response, name)

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

Returns the values of the header specified by name.

Examples

iex> Req.Response.get_header(response, "content-type")
["application/json"]
Link to this function

get_private(response, key, default \\ nil)

View Source
@spec get_private(t(), key :: atom(), default :: term()) :: term()

Gets the value for a specific private key.

Link to this function

get_retry_after(response)

View Source
@spec get_retry_after(t()) :: integer() | nil

Returns the retry-after header delay value or nil if not found.

Link to this function

json(response \\ new(), body)

View Source
@spec json(t(), body :: term()) :: t()

Builds or updates a response with JSON body.

Example

iex> Req.Response.json(%{hello: 42})
%Req.Response{
  status: 200,
  headers: %{"content-type" => ["application/json"]},
  body: ~s|{"hello":42}|
}

iex> resp = Req.Response.new()
iex> Req.Response.json(resp, %{hello: 42})
%Req.Response{
  status: 200,
  headers: %{"content-type" => ["application/json"]},
  body: ~s|{"hello":42}|
}

If the request already contains a 'content-type' header, it is kept as is:

iex> Req.Response.new()
iex> |> Req.Response.put_header("content-type", "application/vnd.api+json; charset=utf-8")
iex> |> Req.Response.json(%{hello: 42})
%Req.Response{
  status: 200,
  headers: %{"content-type" => ["application/vnd.api+json; charset=utf-8"]},
  body: ~s|{"hello":42}|
}
@spec new(options :: keyword() | map() | struct()) :: t()

Returns a new response.

Expects a keyword list, map, or struct containing the response keys.

Example

iex> Req.Response.new(status: 200, body: "body")
%Req.Response{status: 200, headers: %{}, body: "body"}

iex> finch_response = %Finch.Response{status: 200, headers: [{"content-type", "text/html"}]}
iex> Req.Response.new(finch_response)
%Req.Response{status: 200, headers: %{"content-type" => ["text/html"]}, body: ""}
Link to this function

put_header(response, name, value)

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

Adds a new response header name if not present, otherwise replaces the previous value of that header with value.

Examples

iex> resp = Req.Response.put_header(resp, "content-type", "application/json")
iex> resp.headers
[{"content-type", "application/json"}]
Link to this function

put_private(response, key, value)

View Source
@spec put_private(t(), key :: atom(), value :: term()) :: t()

Assigns a private key to value.

Link to this function

update_private(response, key, initial, fun)

View Source
@spec update_private(t(), key :: atom(), default :: term(), (atom() -> term())) :: t()

Updates private key with the given function.

If key is present in request private map then the existing value is passed to fun and its result is used as the updated value of key. If key is not present, default is inserted as the value of key. The default value will not be passed through the update function.

Examples

iex> resp = %Req.Response{private: %{a: 1}}
iex> Req.Response.update_private(resp, :a, 11, & &1 + 1).private
%{a: 2}
iex> Req.Response.update_private(resp, :b, 11, & &1 + 1).private
%{a: 1, b: 11}