<h1>Creating Responses</h1>

Each request handler is expected to return an instance of the <tt>response</tt> structure type that has a SRFI-99 definition similar to the following one:

<verbatim>
  (define-record-type (response message)
    #f #t
    status status-message)
</verbatim>

A <tt>response</tt> is actually a subtype of a <tt>message</tt> (see [Interpreting Parameters]) and inherits its <tt>type</tt>, <tt>headers</tt> and <tt>body</tt> fields. In addition the message specifies the status code and status line message that should be returned to the webserver.

To create a response, you can use any of the following procedures:

<verbatim>
  (make-response STATUS BODY [#:type TYPE] [#:headers HEADERS] [#:status-message STATUS-MESSAGE]) => RESPONSE
</verbatim>
Creates a message given a numeric <tt>STATUS</tt> code and a string <tt>BODY</tt>. Optionally you may specify a content <tt>TYPE</tt> (which you probably should, since the default is additional <tt>HEADERS</tt> or an explicit <tt>STATUS-MESSAGE</tt> (the default is derived from the <tt>STATUS</tt> code).

<verbatim>
  (collect-response STATUS THUNK [#:type TYPE] [#:headers HEADERS] [#:status-message STATUS-MESSAGE]) => RESPONSE
</verbatim>
Works similar to <tt>make-response</tt> except that the message body is computed by invoking <tt>THUNK</tt>, which is expected to write the desired content to the current output port.

<verbatim>
  (make-html-response STATUS HTML [#:headers HEADERS] [#:status-message STATUS-MESSAGE]) => RESPONSE
</verbatim>
Works similar to <tt>make-response</tt> except that the message body is derived from a HTML5 rendering of the X-expression <tt>HTML</tt>. This expression must be an <tt>ELEMENT</tt> according to the following grammar:
<verbatim>
  XEXPR   = ELEMENT | CONTENT
  ELEMENT = (list SYMBOL (list (list SYMBOL CONTENT ...) ...) XEXPR ...) ;; element optionally with attributes
          | (list SYMBOL XEXPR ...)                                      ;; element without attributes
  CONTENT = STRING                                                       ;; character data
          | SYMBOL                                                       ;; symbolic entity reference
          | INTEGER                                                      ;; numeric entity reference
</verbatim>
The content type of the resulting message is

<verbatim>
  (make-error-response STATUS MESSAGE [#:headers HEADERS] [#:status-message STATUS-MESSAGE]) => RESPONSE
</verbatim>
Works similar to <tt>make-html-response</tt> but uses a predefined content document into which it only inserts the given error <tt>MESSAGE</tt>.