Response

is a response data type. It holds the code of the request, the version, the message, the cookies, the headers, and the body.

bad-request

defn

(Fn [] Response)

                        (bad-request)
                    

creates a 400 Bad Request response.

body

instantiate

(Fn [(Ref Response a)] (Ref String a))

gets the body property of a Response.

chunked

defn

(Fn [Int, String, (Ref (Array String) a)] Response)

                        (chunked code content-type chunks)
                    

creates a response with Transfer-Encoding: chunked.

The body is built from an array of chunk strings, each encoded with the chunked framing (<hex-length>\r\n<data>\r\n) and terminated by a final 0\r\n\r\n.

(Response.chunked 200 @"text/plain"
  &[@"first chunk" @"second chunk"])

The chunks are pre-encoded into a single buffer. For truly lazy streaming where chunks are generated on demand, a future version will provide a callback-based API.

client-error?

defn

(Fn [(Ref Response a)] Bool)

                        (client-error? r)
                    

checks whether the status code is between 400 and 500.

code

instantiate

(Fn [(Ref Response a)] (Ref Int a))

gets the code property of a Response.

content-type-for

defn

(Fn [(Ref String a)] String)

                        (content-type-for path)
                    

infers an HTTP Content-Type for a file path based on its extension. Unknown extensions return application/octet-stream.

cookies

instantiate

(Fn [(Ref Response a)] (Ref (Array Cookie) a))

gets the cookies property of a Response.

copy

instantiate

(Fn [(Ref Response a)] Response)

copies a Response.

delete

instantiate

(Fn [Response] ())

deletes a Response. Should usually not be called manually.

file

defn

(Fn [(Ref String a)] Response)

                        (file path)
                    

creates a 200 OK response by reading path from disk. Returns a 404 Not Found response if the file is missing or unreadable. The Content-Type header is inferred from the file extension via content-type-for.

defn

(Fn [(Ref Response a), (Ref String b)] (Maybe String))

                        (header r name)
                    

gets the first value of a header by name (case-insensitive). Returns (Maybe String).

headers

instantiate

(Fn [(Ref Response a)] (Ref (Map String (Array String)) a))

gets the headers property of a Response.

html

defn

(Fn [String] Response)

                        (html body)
                    

creates a 200 OK response with an HTML body.

init

instantiate

(Fn [Int, String, String, (Array Cookie), (Map String (Array String)), String] Response)

creates a Response.

json

defn

(Fn [(Ref JSON a)] Response)

                        (json j)
                    

creates a 200 OK response with a JSON body.

message

instantiate

(Fn [(Ref Response a)] (Ref String a))

gets the message property of a Response.

not-found

defn

(Fn [] Response)

                        (not-found)
                    

creates a 404 Not Found response.

ok?

defn

(Fn [(Ref Response a)] Bool)

                        (ok? r)
                    

checks whether the status code is less than 400.

parse

defn

(Fn [(Ref String a)] (Result Response String))

                        (parse txt)
                    

parses a HTTP response from a string txt.

Returns a (Error String) holding the error message if it fails, otherwise it will return a (Success Response).

prn

instantiate

(Fn [(Ref Response a)] String)

converts a Response to a string.

redirect

defn

(Fn [String] Response)

                        (redirect url)
                    

creates a 302 redirect response to url.

sendfile

defn

(Fn [(Ref String a)] Response)

                        (sendfile path)
                    

creates a 200 OK response that signals the server to transfer the file at path directly to the socket via sendfile(2), avoiding the user-space copy. Falls back to file if the server does not detect the X-Sendfile header.

The Content-Type is inferred from the extension. The server sets Content-Length from the file size.

server-error?

defn

(Fn [(Ref Response a)] Bool)

                        (server-error? r)
                    

checks whether the status code is between 500 and 600.

set-body

instantiate

(Fn [Response, String] Response)

sets the body property of a Response.

set-body!

instantiate

(Fn [(Ref Response a), String] ())

sets the body property of a Response in place.

set-code

instantiate

(Fn [Response, Int] Response)

sets the code property of a Response.

set-code!

instantiate

(Fn [(Ref Response a), Int] ())

sets the code property of a Response in place.

defn

(Fn [Response, Cookie] Response)

                        (set-cookie resp cookie)
                    

adds a Set-Cookie header to the response. The cookie argument is a Cookie value from the http library.

(Response.set-cookie resp
  (Cookie.init @"session" @"abc" @"/"
    (Maybe.Nothing) (Maybe.Nothing) true true (SameSite.Lax)))

For simple name/value cookies, use set-simple-cookie.

set-cookies

instantiate

(Fn [Response, (Array Cookie)] Response)

sets the cookies property of a Response.

set-cookies!

instantiate

(Fn [(Ref Response a), (Array Cookie)] ())

sets the cookies property of a Response in place.

set-headers

instantiate

(Fn [Response, (Map String (Array String))] Response)

sets the headers property of a Response.

set-headers!

instantiate

(Fn [(Ref Response a), (Map String (Array String))] ())

sets the headers property of a Response in place.

set-message

instantiate

(Fn [Response, String] Response)

sets the message property of a Response.

set-message!

instantiate

(Fn [(Ref Response a), String] ())

sets the message property of a Response in place.

defn

(Fn [Response, String, String] Response)

                        (set-simple-cookie resp name value)
                    

adds a Set-Cookie header with a name and value. Uses Path=/, HttpOnly, and SameSite=Lax as defaults.

set-version

instantiate

(Fn [Response, String] Response)

sets the version property of a Response.

set-version!

instantiate

(Fn [(Ref Response a), String] ())

sets the version property of a Response in place.

str

defn

(Fn [(Ref Response a)] String)

                        (str r)
                    

stringifies the response as it looks on the wire.

text

defn

(Fn [String] Response)

                        (text body)
                    

creates a 200 OK response with a plain text body.

update-body

instantiate

(Fn [Response, (Ref (Fn [String] String a) b)] Response)

updates the body property of a Response using a function f.

update-code

instantiate

(Fn [Response, (Ref (Fn [Int] Int a) b)] Response)

updates the code property of a Response using a function f.

update-cookies

instantiate

(Fn [Response, (Ref (Fn [(Array Cookie)] (Array Cookie) a) b)] Response)

updates the cookies property of a Response using a function f.

update-headers

instantiate

(Fn [Response, (Ref (Fn [(Map String (Array String))] (Map String (Array String)) a) b)] Response)

updates the headers property of a Response using a function f.

update-message

instantiate

(Fn [Response, (Ref (Fn [String] String a) b)] Response)

updates the message property of a Response using a function f.

update-version

instantiate

(Fn [Response, (Ref (Fn [String] String a) b)] Response)

updates the version property of a Response using a function f.

version

instantiate

(Fn [(Ref Response a)] (Ref String a))

gets the version property of a Response.

with-header

defn

(Fn [Response, String, String] Response)

                        (with-header r k v)
                    

adds a header to a response.

with-status

defn

(Fn [Response, Int, String] Response)

                        (with-status r code msg)
                    

sets the status code and message of a response.