I'm trying to create an endpoint that will send a status code using caveman2.
(defroute "/books/" ()
(render-json "Hello"))
I have found the function throw-code
which enables me to send a status code, but it won't let me send a response body as well. It seems it was meant for handling errors.
(import 'caveman2:throw-code)
(defroute "/books/" ()
(throw-code 403)
;; This will not respond with "Hello"
(render-json "Hello"))
The caveman2 github page talks about responding with specific status codes:
Set HTTP headers or HTTP status
There are several special variables available during a HTTP request. request and response represent a request and a response. If you are familiar with Clack, these are instances of subclasses of Clack.Request and Clack.Response.
(use-package :caveman2)
;; Get a value of Referer header.
(http-referer *request*)
;; Set Content-Type header.
(setf (getf (response-headers *response*) :content-type) "application/json")
;; Set HTTP status.
(setf (status *response*) 304)
However, I do not know how to instantiate these objects or how to import them. I couldn't find a working example and when I put this in as is in my code it won't compile.
How can I accomplish this?