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 acceptsatom/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
Link to this section Functions
Returns the values of the header specified by key
.
examples
Examples
iex> Req.Response.get_header(response, "content-type")
["application/json"]
Gets the value for a specific private key
.
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}|
}
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: ""}
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"}]
Assigns a private key
to value
.