March Docs

Response

Bastion.Response — response-building helpers.

All helpers accept an HttpServer.Conn and return an HttpServer.Conn with the appropriate content-type header set and the conn halted (via send_resp).

These are thin wrappers around HttpServer's built-in response functions with additional convenience helpers for common status codes.

Functions

fnbad_requestbad_request(conn : Conn, message : String) : Conn#

Send 400 Bad Request with a descriptive message.

fncreatedcreated(conn : Conn, body : String) : Conn#

Send 201 Created with a plain-text body.

fnforbiddenforbidden(conn : Conn, message : String) : Conn#

Send 403 Forbidden with the given message.

fnhtmlhtml(conn : Conn, status : Int, body : String) : Conn#

Send an HTML response.

Sets content-type to \"text/html; charset=utf-8\", writes body, halts conn.

Example:
  Response.html(conn, 200, \"<h1>Hello</h1>\")
fninternal_errorinternal_error(conn : Conn, message : String) : Conn#

Send 500 Internal Server Error with the given message.

fninternal_error_internal_error_(conn : Conn) : Conn#

Send 500 Internal Server Error with the default body \

fninternal_server_error_internal_server_error_(conn : Conn) : Conn#

Alias for internal_error_.

fnjsonjson(conn : Conn, status : Int, body : String) : Conn#

Send a JSON response.

Sets content-type to \"application/json\", writes body, halts conn.
The caller is responsible for serialising the body to a JSON string
(e.g. with Json.encode).

Example:
  Response.json(conn, 200, Json.encode(my_map))
fnmethod_not_allowedmethod_not_allowed(conn : Conn) : Conn#

Send 405 Method Not Allowed.

fnno_contentno_content(conn : Conn) : Conn#

Send 204 No Content (empty body).

fnnot_foundnot_found(conn : Conn, message : String) : Conn#

Send 404 Not Found with the given message.

fnnot_found_not_found_(conn : Conn) : Conn#

Send 404 Not Found with the default body \

fnokok(conn : Conn, body : String) : Conn#

Send 200 OK with a plain-text body.

fnrawraw(conn : Conn, status : Int, body : String) : Conn#

Send a raw response with an explicit status, without setting a content-type.

Useful when the content-type has already been set via put_resp_header, or
for responses with no body (e.g. 204 No Content).
fnredirectredirect(conn : Conn, url : String) : Conn#

Send a 302 Found redirect to the given URL.

Example:
  Response.redirect(conn, \"/login\")
fnredirect_permanentredirect_permanent(conn : Conn, url : String) : Conn#

Send a 301 Moved Permanently redirect to the given URL. Use for permanent URL changes (canonicalisation, HTTPS upgrades, etc.).

fnservice_unavailableservice_unavailable(conn : Conn) : Conn#

Send 503 Service Unavailable.

fntexttext(conn : Conn, status : Int, body : String) : Conn#

Send a plain-text response.

Sets content-type to \"text/plain; charset=utf-8\", writes body, halts conn.
fnunauthorizedunauthorized(conn : Conn, message : String) : Conn#

Send 401 Unauthorized with the given message.

fnunprocessableunprocessable(conn : Conn, message : String) : Conn#

Send 422 Unprocessable Entity with the given message.