Commit65eb6926Recorded6 Jul 2026Repositorysigil-http

http-response/json: accept a dict or an already-encoded string

Message

Let http-response/json take either a dict (encoded for the caller with json-encode) or an already-encoded JSON string (passed through untouched), so handlers can return #{ ... } directly without calling json-encode.

Add sigil-json as a hard dependency and import (sigil json) in response.sgl. The hard dep avoids native whole-program DCE tree-shaking an eval-only optional-load module. Update the type spec to (any-of string? dict?) and refresh the docstring to show both forms.

Changed
 package.sgl                 |  3 ++-
 src/sigil/http/response.sgl | 13 ++++++++-----
 2 files changed, 10 insertions(+), 6 deletions(-)
Diff
package.sglmodified
@@ -14,7 +14,8 @@
14
15
dependencies: (list
16
(from-git url: "codeberg:sigil/sigil-socket" version: "^0.16.0")
17
(from-git url: "codeberg:sigil/sigil-tls" version: "^0.16.0"))
+17
(from-git url: "codeberg:sigil/sigil-tls" version: "^0.16.0")
+18
(from-git url: "codeberg:sigil/sigil-json" version: "^0.16.0"))
19
20
dev-dependencies: (list
21
(from-git url: "codeberg:sigil/sigil" package: "sigil-test" version: "^0.17")
src/sigil/http/response.sglmodified
@@ -12,6 +12,7 @@
12
(sigil time)
13
(sigil fs)
14
(sigil archive) ; gzip-compress
+15
(sigil json) ; json-encode for http-response/json
16
(sigil http mime))
17
18
(export
@@ -192,17 +193,19 @@
193
194
;;; Create a JSON response.
195
;;;
195
;;; The body should be an already-encoded JSON string.
+196
;;; `body` is either a dict (encoded for you with `json-encode`) or an
+197
;;; already-encoded JSON string (passed through untouched).
198
;;;
199
;;; ```scheme
198
;;; (http-response/json 200 (json-encode #{ name: "Alice" }))
+200
;;; (http-response/json 200 #{ name: "Alice" }) ; dict, encoded for you
+201
;;; (http-response/json 200 (json-encode #{ name: "Alice" })) ; string still works
202
;;; ```
200
(define (http-response/json status json-string)
201
(: integer? string? -> http-response?)
+203
(define (http-response/json status body)
+204
(: integer? (any-of string? dict?) -> http-response?)
205
(http-response
206
status: status
207
headers: #{ content-type: "application/json" }
205
body: json-string))
+208
body: (if (string? body) body (json-encode body))))
209
210
;;; Create a 404 Not Found response with a default HTML body.
211
(define (http-response/not-found)