sigildocs

(sigil http response)

(sigil http response) - HTTP Response Types and Serialization

Provides the <http-response> record and functions for serializing HTTP/1.1 responses to sockets.

Exports

http-responseprocedure

HTTP Response Record

Test if a value is a http-response struct.

Get the status field of a http-response struct.

Get the headers field of a http-response struct.

Get the body field of a http-response struct.

Get human-readable status message

Convenience constructors

body-lengthprocedure

Get body length for Content-Length header For strings, we need byte length (UTF-8), not character count

Check if body is a streaming producer

Build status line: "HTTP/1.1 200 OKrn"

Build headers string Converts keyword keys to HTTP header format

Ensure required headers are present

Write HTTP response to socket write-fn: (socket data) -> bytes-written or #f Returns #t on success, #f on error

Convert response to string (for debugging)

Create a Server-Sent Events response.

The producer is called with two arguments:

  • send: (lambda (event-type data) ...) - sends an event
  • close: (lambda () ...) - closes the stream

Example:

(http-response/sse
  (lambda (send close)
    (send "message" (dict text: "Hello"))
    (send "update" (dict count: 42))
    ;; Keep connection open, send more events...
    ))
sse-eventprocedure

Format a Server-Sent Event with event type and data.

Returns a string in SSE format: event: <type> data: <json>

The data is JSON-encoded if it's not already a string.

sse-dataprocedure

Format a simple data-only SSE event (no event type).

Returns a string in SSE format: data: <content>