KScript docs
EN

Routes and APIs

Responses

@response(), @view() and @redirect() give a response value, and middleware gets one back from $next($request). You may read it and change it:

  • Read it with .status, .body, .type, .headers, ->header(name), .is_ok and ->convert(from: json).
  • Change it with ->with_status(code), ->with_header(name, value), ->with_type(type), ->with_etag(value) and ->with_cache(directives). Each returns the response, so the calls chain.
  • ->with_cookie(name, value) sets a cookie, and ->without_cookie(name) deletes one. A cookie is HttpOnly and SameSite=Lax unless you say otherwise, Secure on https, and lasts until the browser closes unless expires: says how long: ->with_cookie('theme', 'dark', expires: 30d, not http_only). path:, domain:, secure and same_site: lax|strict|none are there too.
  • ->with_header('Set-Cookie', ...) adds a raw cookie header each time it is called, rather than replacing the last, and each cookie is sent as a Set-Cookie header of its own.
  • @response($answer) given a response returns it as it is, with its status, and @response($answer, status: 410) changes the status.

kscript serve sends Server: kscript on HTTP responses. To send your own value for a route, use ->with_header('Server', 'my-app').