KScript docs
EN

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

FieldTypeDescription
.bodystringThe body as text.
.headersmapThe headers as a map.
.is_okboolTrue when the status is 2xx.
.statusnumberThe HTTP status code, such as 200 or 404.
.typestringThe Content-Type.

From KScript.Http

These need <import package="KScript.Http" />import KScript.Http. See KScript.Http.

#http_response->header(name) → string KScript.Http

One header (or null). For Set-Cookie set more than once, the first; .headers has them all.
Example
<let name="response" value="@redirect('/login')" />
<echo>{ $response.status } { $response->header('Location') }</echo>
$response = @redirect('/login')
echo "{ $response.status } { $response->header('Location') }"

#http_response->with_cache(directives) → http_response KScript.Http

Sets Cache-Control: ->with_cache('public, max-age=300'), or a number of seconds: ->with_cache(300).
Example
<fn name="index" params="request">
  <return value="@view('posts/index')->with_cache(300)" />
</fn>
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).
ParameterTakes
name requiredany value
value requiredany value
expires optionalduration
path optionalany value
domain optionalany value
http_only optionala flag: http_only or not http_only
secure optionala flag: secure or not secure
same_site optionalone of these words: lax|strict|none

Arguments can be named: ->with_cookie(…, value: …, expires: …).

Example
<fn name="remember_theme" params="request">
  <return value="@redirect('/')->with_cookie('theme', 'dark', expires: 30d)" />
</fn>
fn remember_theme(request) {
  return @redirect('/')->with_cookie('theme', 'dark', expires: 30d)
}

#http_response->with_etag(value?, weak?: bool) → http_response KScript.Http

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.
ParameterTakes
value optionalany value
weak optionala flag: weak or not weak

Arguments can be named: ->with_etag(…, weak: …).

Example
<fn name="show" params="request">
  <return value="@response({ id: 1, title: 'Hello' })->with_etag()" />
</fn>
fn show(request) {
  return @response({ id: 1, title: 'Hello' })->with_etag()
}

#http_response->with_header(name, value) → http_response KScript.Http

Sets a header and returns the response: $next($request)->with_header('X-Frame-Options', 'DENY'). Set-Cookie adds a cookie each time instead of replacing the last.

Arguments can be named: ->with_header(…, value: …).

Example
<fn name="secure_headers" params="request, next">
  <return value="$next($request)->with_header('X-Frame-Options', 'DENY')" />
</fn>
fn secure_headers(request, next) {
  return $next($request)->with_header('X-Frame-Options', 'DENY')
}

#http_response->with_status(code) → http_response KScript.Http

Changes the status code and returns the response.
Example
<fn name="store" params="request">
  <return value="@response({ saved: true })->with_status(201)" />
</fn>
fn store(request) {
  return @response({ saved: true })->with_status(201)
}

#http_response->with_type(type) → http_response KScript.Http

Changes the Content-Type and returns the response.
Example
<fn name="export" params="request">
  <return value="@response('name,email')->with_type('text/csv')" />
</fn>
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
<fn name="forget_theme" params="request">
  <return value="@redirect('/')->without_cookie('theme')" />
</fn>
fn forget_theme(request) {
  return @redirect('/')->without_cookie('theme')
}

Methods from any