httpd/response
Response struct, constructors, and mutators.
Since: H4
with-header
(with-header [resp : Response key : cstr val : cstr] :)
append a header to a Response (mutates in place).
| resp | :int Response handle | |
| key | header name | |
| val | header value |
:int -- the same Response handle (for chaining)
(-> (ok "text/plain" "hi")
(with-header "X-Hello" "world"))
Since: H4
with-header-set
(with-header-set [resp : Response key : cstr val : cstr] :)
set a header on a Response, replacing any existing
| resp | Response handle | |
| key | header name | |
| val | header value |
Response -- the same handle (for chaining)
(with-header-set (resp-ok "text/plain" "{}") "Content-Type" "application/json")
Since: tourist-on-stdlib-httpd H2
resp-header
(resp-header [resp : Response name : cstr] :)
read a header of a Response by name (case-insensitive).
| resp | Response handle | |
| name | header name |
(Option cstr) -- some(value), or none when the header is absent. The value belongs to the Response.
(resp-header (resp-ok "text/plain" "hi") "Content-Type") ; => some("text/plain")
Since: tourist-on-stdlib-httpd H2
with-body
(with-body [resp : Response body : cstr] :)
replace the body of a Response (mutates in place).
| resp | :int Response handle | |
| body | new body string |
:int -- the same Response handle
(-> (ok "text/plain" "") (with-body "later"))
Since: H4
with-body-bytes
(with-body-bytes [resp : Response bytes ptr<void> len : int] :)
replace the body of a Response with raw bytes.
| resp | Response handle | |
| bytes | source buffer | |
| len | byte count |
Response -- the same handle
(with-body-bytes (resp-ok "image/png" "") png-data png-len)
Since: tourist-on-stdlib-httpd H2
with-status
(with-status [resp : Response status : int] :)
replace the status code of a Response (mutates in place).
| resp | :int Response handle | |
| status | new HTTP status code |
:int -- the same Response handle
(-> (ok "text/plain" "no") (with-status 503))
Since: H4
free-response
(free-response [resp : Response] :)
release a Response handle and all owned storage.
| resp | :int Response handle, or 0 (no-op) |
:void
(free-response r)
Since: H4
response
(response [status : int content-type : cstr body : cstr] :)
build an HTTP response with an arbitrary status code.
| status | HTTP status code, e.g. 200, 201, 500 | |
| content-type | value for the Content-Type header, e.g. "text/html" | |
| body | response body string ("" for no body) |
:int -- Response handle. Free with free-response, or hand to the server which takes ownership on send.
(response 201 "application/json" "{\"id\":1}")
Since: H4
resp-ok
(resp-ok [content-type : cstr body : cstr] :)
build a 200 OK response.
| content-type | value for the Content-Type header | |
| body | response body string |
:int -- Response handle with status 200
(resp-ok "text/plain" "Hello, world!")
Since: H4
not-found
(not-found [body : cstr] :)
build a 404 Not Found response with a text/plain body.
| body | body string (typically a short message) |
:int -- Response handle with status 404
(not-found "no such resource")
Since: H4
bad-request
(bad-request [body : cstr] :)
build a 400 Bad Request response with a text/plain body.
| body | body string (typically a short error message) |
:int -- Response handle with status 400
(bad-request "missing id parameter")
Since: H4
Internal definitions
__resp-new