http_response fields and methods Responses made by controllers and middleware: @response(), @view(), @redirect().
A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).
Fields Field Type Description .bodystring The body as text. .is_okbool True when the status is 2xx. .statusnumber The HTTP status code, such as 200 or 404. .typestring The Content-Type.
From KScript.Http These need <import package="KScript.Http" />import KScript.Http . See KScript.Http .
Sets Cache-Control: ->with_cache('public, max-age=300'), or a number of seconds: ->with_cache(300).
Example
.kscript XML Copy
<fn name="index" params="request">
<return value="@view('posts/index')->with_cache(300)" />
</fn>.kscript XML Copy
fn index(request) {
return @view('posts/index')->with_cache(300)
}# http_response ->with_cookie (name , value , expires?: duration , path? , domain? , http_only?: bool , secure?: bool , same_site?: lax|strict|none ) → http_response KScript.Http Sets a cookie and returns the response. The cookie is HttpOnly and SameSite=Lax unless told otherwise, Secure on https, and lasts for the browser session unless expires: is given (such as 30d).
Parameter Takes name required any value value required any value expires optional duration path optional any value domain optional any value http_only optional a flag: http_only or not http_only secure optional a flag: secure or not secure same_site optional one of these words: lax|strict|none
Arguments can be named: ->with_cookie(…, value: …, expires: …).
Example
.kscript XML Copy
<fn name="remember_theme" params="request">
<return value="@redirect('/')->with_cookie('theme', 'dark', expires: 30d)" />
</fn>.kscript XML Copy
fn remember_theme(request) {
return @redirect('/')->with_cookie('theme', 'dark', expires: 30d)
}Sets the ETag: a hash of the value, or of the body without one. Add the etag middleware (or etag="true") to answer If-None-Match with 304.
Parameter Takes value optional any value weak optional a flag: weak or not weak
Arguments can be named: ->with_etag(…, weak: …).
Example
.kscript XML Copy
<fn name="show" params="request">
<return value="@response({ id: 1, title: 'Hello' })->with_etag()" />
</fn>.kscript XML Copy
fn show(request) {
return @response({ id: 1, title: 'Hello' })->with_etag()
}Changes the status code and returns the response.
Example
.kscript XML Copy
<fn name="store" params="request">
<return value="@response({ saved: true })->with_status(201)" />
</fn>.kscript XML Copy
fn store(request) {
return @response({ saved: true })->with_status(201)
}Changes the Content-Type and returns the response.
Example
.kscript XML Copy
<fn name="export" params="request">
<return value="@response('name,email')->with_type('text/csv')" />
</fn>.kscript XML Copy
fn export(request) {
return @response('name,email')->with_type('text/csv')
}# http_response ->without_cookie (name , path? , domain? ) → http_response KScript.Http Tells the browser to delete a cookie and returns the response. Give the path and domain it was set with.
Arguments can be named: ->without_cookie(…, path: …, domain: …).
Example
.kscript XML Copy
<fn name="forget_theme" params="request">
<return value="@redirect('/')->without_cookie('theme')" />
</fn>.kscript XML Copy
fn forget_theme(request) {
return @redirect('/')->without_cookie('theme')
}Methods from any