nhttp_resp (nhttp v1.0.0)

View Source

HTTP response builders for nhttp.

Provides convenience functions for building HTTP responses with proper status codes and reason phrases. Responses are nhttp_lib:response() maps.

Example

handle_request(#{method := get, path := <<"/">>}, State) ->
    {reply, nhttp_resp:ok(<<"Hello!">>), State};
handle_request(_Req, State) ->
    {reply, nhttp_resp:not_found(), State}.

For streaming responses, see nhttp_stream.

Summary

Functions

Create a 400 Bad Request response.

Create a 400 Bad Request response with body.

Create a 409 Conflict response.

Create a 409 Conflict response with body.

Create a 201 Created response with Location header.

Create a 201 Created response with Location header and body.

Create a 403 Forbidden response.

Create a 403 Forbidden response with body.

Create a 302 Found response.

Create a 410 Gone response.

Create a 500 Internal Server Error response.

Create a 500 Internal Server Error response with body.

Create a 405 Method Not Allowed response with Allow header.

Create a 301 Moved Permanently response.

Create a response with status code only.

Create a response with status code and body.

Create a response with status code, headers, and body.

Create a 204 No Content response.

Create a 404 Not Found response.

Create a 404 Not Found response with body.

Create a 501 Not Implemented response.

Create a 200 OK response with no body.

Create a 200 OK response with body.

Create a 200 OK response with headers and body.

Create a 308 Permanent Redirect response.

Get the reason phrase for a status code.

Create a 303 See Other response.

Create a 503 Service Unavailable response.

Create a 503 Service Unavailable response with Retry-After header.

Create a 307 Temporary Redirect response.

Create a 429 Too Many Requests response.

Create a 429 Too Many Requests response with Retry-After header.

Create a 401 Unauthorized response.

Create a 401 Unauthorized response with WWW-Authenticate header.

Create a 422 Unprocessable Content response.

Create a 422 Unprocessable Content response with body.

Functions

bad_request()

-spec bad_request() -> nhttp_lib:response().

Create a 400 Bad Request response.

bad_request(Body)

-spec bad_request(iodata()) -> nhttp_lib:response().

Create a 400 Bad Request response with body.

conflict()

-spec conflict() -> nhttp_lib:response().

Create a 409 Conflict response.

conflict(Body)

-spec conflict(iodata()) -> nhttp_lib:response().

Create a 409 Conflict response with body.

created(Location)

-spec created(binary()) -> nhttp_lib:response().

Create a 201 Created response with Location header.

created(Location, Body)

-spec created(binary(), iodata()) -> nhttp_lib:response().

Create a 201 Created response with Location header and body.

forbidden()

-spec forbidden() -> nhttp_lib:response().

Create a 403 Forbidden response.

forbidden(Body)

-spec forbidden(iodata()) -> nhttp_lib:response().

Create a 403 Forbidden response with body.

found(Location)

-spec found(binary()) -> nhttp_lib:response().

Create a 302 Found response.

gone()

-spec gone() -> nhttp_lib:response().

Create a 410 Gone response.

internal_error()

-spec internal_error() -> nhttp_lib:response().

Create a 500 Internal Server Error response.

internal_error(Body)

-spec internal_error(iodata()) -> nhttp_lib:response().

Create a 500 Internal Server Error response with body.

method_not_allowed(AllowedMethods)

-spec method_not_allowed([binary()]) -> nhttp_lib:response().

Create a 405 Method Not Allowed response with Allow header.

moved_permanently(Location)

-spec moved_permanently(binary()) -> nhttp_lib:response().

Create a 301 Moved Permanently response.

new(Status)

Create a response with status code only.

new(Status, Body)

Create a response with status code and body.

new(Status, Headers, Body)

Create a response with status code, headers, and body.

no_content()

-spec no_content() -> nhttp_lib:response().

Create a 204 No Content response.

not_found()

-spec not_found() -> nhttp_lib:response().

Create a 404 Not Found response.

not_found(Body)

-spec not_found(iodata()) -> nhttp_lib:response().

Create a 404 Not Found response with body.

not_implemented()

-spec not_implemented() -> nhttp_lib:response().

Create a 501 Not Implemented response.

ok()

-spec ok() -> nhttp_lib:response().

Create a 200 OK response with no body.

ok(Body)

-spec ok(iodata()) -> nhttp_lib:response().

Create a 200 OK response with body.

ok(Headers, Body)

Create a 200 OK response with headers and body.

permanent_redirect(Location)

-spec permanent_redirect(binary()) -> nhttp_lib:response().

Create a 308 Permanent Redirect response.

reason_phrase/1

-spec reason_phrase(nhttp_lib:status()) -> binary().

Get the reason phrase for a status code.

see_other(Location)

-spec see_other(binary()) -> nhttp_lib:response().

Create a 303 See Other response.

service_unavailable()

-spec service_unavailable() -> nhttp_lib:response().

Create a 503 Service Unavailable response.

service_unavailable(RetryAfterSecs)

-spec service_unavailable(non_neg_integer()) -> nhttp_lib:response().

Create a 503 Service Unavailable response with Retry-After header.

temporary_redirect(Location)

-spec temporary_redirect(binary()) -> nhttp_lib:response().

Create a 307 Temporary Redirect response.

too_many_requests()

-spec too_many_requests() -> nhttp_lib:response().

Create a 429 Too Many Requests response.

too_many_requests(RetryAfterSecs)

-spec too_many_requests(non_neg_integer()) -> nhttp_lib:response().

Create a 429 Too Many Requests response with Retry-After header.

unauthorized()

-spec unauthorized() -> nhttp_lib:response().

Create a 401 Unauthorized response.

unauthorized(Challenge)

-spec unauthorized(binary()) -> nhttp_lib:response().

Create a 401 Unauthorized response with WWW-Authenticate header.

unprocessable_content()

-spec unprocessable_content() -> nhttp_lib:response().

Create a 422 Unprocessable Content response.

unprocessable_content(Body)

-spec unprocessable_content(iodata()) -> nhttp_lib:response().

Create a 422 Unprocessable Content response with body.