roadrunner_resp (roadrunner v0.1.0)

View Source

Convenience builders for the buffered {Status, Headers, Body} triple — one of the five shapes a roadrunner handler can return.

Each helper sets Content-Type and Content-Length so handlers don't repeat the boilerplate. Body framing (Connection: close, Transfer-Encoding, etc.) is still the connection layer's job — these helpers only fill in what the handler can know.

These helpers only construct the buffered form (buffered_response/0). Streaming, loop, sendfile, and WebSocket-upgrade handlers build their own tuple directly; see roadrunner_handler:response/0 for the full union.

Summary

Functions

Prepend a header to an existing response triple.

Empty 400 Bad Request response.

Empty 403 Forbidden response.

HTML response with text/html; charset=utf-8.

Empty 500 Internal Server Error response.

JSON response — the term is encoded via the stdlib json module (OTP 27+) and Content-Type is set to application/json.

Empty 204 No Content response.

Empty 404 Not Found response.

Redirect response — sets the Location header and an empty body. Use a 3xx status (typically 301, 302, 303, 307, or 308).

Add a Set-Cookie header to a response — wraps roadrunner_cookie:serialize/3 so handlers don't have to.

Empty-body response with an arbitrary status code — handy for statuses outside the named shortcut set (e.g., 418, 503).

Plain-text response with text/plain; charset=utf-8.

Empty 401 Unauthorized response.

Types

buffered_response()

-type buffered_response() :: {roadrunner_http:status(), roadrunner_http:headers(), iodata()}.

Functions

add_header/3

-spec add_header(buffered_response(), Name :: binary(), Value :: iodata()) -> buffered_response().

Prepend a header to an existing response triple.

The header is added to the front of the list — last-write-wins for any subsequent lookup. Value may be iodata; it is flattened into a binary so the wire encoder doesn't have to.

bad_request()

-spec bad_request() -> buffered_response().

Empty 400 Bad Request response.

forbidden()

-spec forbidden() -> buffered_response().

Empty 403 Forbidden response.

html(StatusCode, Body)

-spec html(StatusCode :: roadrunner_http:status(), Body :: iodata()) -> buffered_response().

HTML response with text/html; charset=utf-8.

The charset suffix is intentional — modern browsers default to ISO-8859-1 absent an explicit charset in the response header, which is wrong for ~all current content. To match a server that emits bare text/html (e.g. cowboy's default), override after the build:

roadrunner_resp:add_header(
    roadrunner_resp:html(200, Body), ~"content-type", ~"text/html"
).

add_header/3 prepends, so the bare value wins on header lookup.

internal_error()

-spec internal_error() -> buffered_response().

Empty 500 Internal Server Error response.

json(StatusCode, Term)

-spec json(StatusCode :: roadrunner_http:status(), Term :: term()) -> buffered_response().

JSON response — the term is encoded via the stdlib json module (OTP 27+) and Content-Type is set to application/json.

no_content()

-spec no_content() -> buffered_response().

Empty 204 No Content response.

not_found()

-spec not_found() -> buffered_response().

Empty 404 Not Found response.

redirect(StatusCode, Location)

-spec redirect(StatusCode :: roadrunner_http:redirect_status(), Location :: binary()) ->
                  buffered_response().

Redirect response — sets the Location header and an empty body. Use a 3xx status (typically 301, 302, 303, 307, or 308).

set_cookie(Resp, Name, Value, Opts)

-spec set_cookie(buffered_response(),
                 Name :: binary(),
                 Value :: binary(),
                 roadrunner_cookie:serialize_opts()) ->
                    buffered_response().

Add a Set-Cookie header to a response — wraps roadrunner_cookie:serialize/3 so handlers don't have to.

status(Code)

Empty-body response with an arbitrary status code — handy for statuses outside the named shortcut set (e.g., 418, 503).

text(StatusCode, Body)

-spec text(StatusCode :: roadrunner_http:status(), Body :: iodata()) -> buffered_response().

Plain-text response with text/plain; charset=utf-8.

unauthorized()

-spec unauthorized() -> buffered_response().

Empty 401 Unauthorized response.