nhttp_resp (nhttp v1.0.0)
View SourceHTTP 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
-spec bad_request() -> nhttp_lib:response().
Create a 400 Bad Request response.
-spec bad_request(iodata()) -> nhttp_lib:response().
Create a 400 Bad Request response with body.
-spec conflict() -> nhttp_lib:response().
Create a 409 Conflict response.
-spec conflict(iodata()) -> nhttp_lib:response().
Create a 409 Conflict response with body.
-spec created(binary()) -> nhttp_lib:response().
Create a 201 Created response with Location header.
-spec created(binary(), iodata()) -> nhttp_lib:response().
Create a 201 Created response with Location header and body.
-spec forbidden() -> nhttp_lib:response().
Create a 403 Forbidden response.
-spec forbidden(iodata()) -> nhttp_lib:response().
Create a 403 Forbidden response with body.
-spec found(binary()) -> nhttp_lib:response().
Create a 302 Found response.
-spec gone() -> nhttp_lib:response().
Create a 410 Gone response.
-spec internal_error() -> nhttp_lib:response().
Create a 500 Internal Server Error response.
-spec internal_error(iodata()) -> nhttp_lib:response().
Create a 500 Internal Server Error response with body.
-spec method_not_allowed([binary()]) -> nhttp_lib:response().
Create a 405 Method Not Allowed response with Allow header.
-spec moved_permanently(binary()) -> nhttp_lib:response().
Create a 301 Moved Permanently response.
-spec new(nhttp_lib:status()) -> nhttp_lib:response().
Create a response with status code only.
-spec new(nhttp_lib:status(), iodata()) -> nhttp_lib:response().
Create a response with status code and body.
-spec new(nhttp_lib:status(), nhttp_lib:headers(), iodata()) -> nhttp_lib:response().
Create a response with status code, headers, and body.
-spec no_content() -> nhttp_lib:response().
Create a 204 No Content response.
-spec not_found() -> nhttp_lib:response().
Create a 404 Not Found response.
-spec not_found(iodata()) -> nhttp_lib:response().
Create a 404 Not Found response with body.
-spec not_implemented() -> nhttp_lib:response().
Create a 501 Not Implemented response.
-spec ok() -> nhttp_lib:response().
Create a 200 OK response with no body.
-spec ok(iodata()) -> nhttp_lib:response().
Create a 200 OK response with body.
-spec ok(nhttp_lib:headers(), iodata()) -> nhttp_lib:response().
Create a 200 OK response with headers and body.
-spec permanent_redirect(binary()) -> nhttp_lib:response().
Create a 308 Permanent Redirect response.
-spec reason_phrase(nhttp_lib:status()) -> binary().
Get the reason phrase for a status code.
-spec see_other(binary()) -> nhttp_lib:response().
Create a 303 See Other response.
-spec temporary_redirect(binary()) -> nhttp_lib:response().
Create a 307 Temporary Redirect response.
-spec too_many_requests() -> nhttp_lib:response().
Create a 429 Too Many Requests response.
-spec too_many_requests(non_neg_integer()) -> nhttp_lib:response().
Create a 429 Too Many Requests response with Retry-After header.
-spec unauthorized() -> nhttp_lib:response().
Create a 401 Unauthorized response.
-spec unauthorized(binary()) -> nhttp_lib:response().
Create a 401 Unauthorized response with WWW-Authenticate header.
-spec unprocessable_content() -> nhttp_lib:response().
Create a 422 Unprocessable Content response.
-spec unprocessable_content(iodata()) -> nhttp_lib:response().
Create a 422 Unprocessable Content response with body.