View Source Req.Response (req v0.3.10)

The response struct.

Fields:

  • :status - the HTTP status code

  • :headers - the HTTP response headers

  • :body - the HTTP response body

  • :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.

Link to this section Summary

Functions

Returns the values of the header specified by key.

Gets the value for a specific private key.

Builds or updates a response with JSON body.

Returns a new response.

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

Assigns a private key to value.

Link to this section Types

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

Link to this section Functions

Link to this function

get_header(response, key)

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

Returns the values of the header specified by key.

examples

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

json(response \\ new(), body)

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

Builds or updates a response with JSON body.

example

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

Example

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

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

put_header(response, key, value)

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

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

examples

Examples

iex> Req.Response.put_header(response, "content-type", "application/json").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.