Calling your own API
@api() from KScript.Net, given a path instead of an address, asks the project's own routes, in the same process and without the network:
<import package="KScript.Net" />
<let name="notes" value="@api(/api/notes)->check()->convert(from: json)" />
<let name="added" value="@api(/api/notes, post: { title: 'Ship it' })" />
<echo value="$added.status" /> <!-- 201 -->import KScript.Net
$notes = @api(/api/notes)->check()->convert(from: json)
$added = @api(/api/notes, post: { title: 'Ship it' })
echo $added.status // 201It works from pages, tests and scripts in the project (the folder with kscript.json), and from routes. The arguments and the response are the same as for an address: post: and the other methods, options: { headers: … }, then .status, ->convert(from: json), ->header(name) and the rest. A path with no route is a 404.
The method and its body are one named argument, and the options people set most have one too:
| Argument | What it does |
|---|---|
post: { title: 'Ship it' }, put: $note, patch: $changes |
Sends that method with this body. A map or list is sent as JSON. Without one, the request is a GET. |
delete: true |
Sends a DELETE with no body. Anything else is sent as the body. |
bearer: $token |
Adds Authorization: Bearer … to the headers. |
timeout: 5s, retries: 3 |
The same as those fields in options. |
as: Note |
The response must succeed, and its JSON becomes Note records. |
encoding: form, encoding: multipart |
Sends a map body as an HTML form (application/x-www-form-urlencoded) or as multipart/form-data instead of JSON. |
query: { page: 2 } |
Adds values to the URL's query string. A list is sent once per item. |
Many APIs want a form rather than JSON, such as an OAuth token endpoint, and file uploads want multipart. In a multipart body a base64 value is sent as a file, and an uploaded file from $request.files keeps its name and type, so a route can pass an upload on:
<import package="KScript.Net" />
<let name="token" value="@api('https://auth.example.com/token', post: { grant_type: 'client_credentials', client_id: $id }, encoding: form)->convert(from: json)" />
<do>@api('https://files.example.com/upload', post: { title: 'Avatar', file: $files.avatar[0] }, encoding: multipart, bearer: $token.access_token)</do>
<do>@api('https://api.example.com/search', query: { q: 'cats', tag: ['a', 'b'] })</do>import KScript.Net
$token = @api('https://auth.example.com/token', post: { grant_type: 'client_credentials', client_id: $id }, encoding: form)->convert(from: json)
@api('https://files.example.com/upload', post: { title: 'Avatar', file: $files.avatar[0] }, encoding: multipart, bearer: $token.access_token)
@api('https://api.example.com/search', query: { q: 'cats', tag: ['a', 'b'] })Your own routes take encoding: form and JSON; multipart is for addresses on the network.
post: null and delete: false send nothing, so a helper may pass its own arguments straight on: @api($url, post: $post, delete: $delete). An option given both ways is an error, so a request says each thing once.
@api() sends User-Agent: kscript by default, to remote and local addresses alike. To name your own client, pass options: { headers: { 'User-Agent': 'my-app' } }, which applies to that one request.
Cookies and sign-in
A request made while answering another one, such as a page calling the API or a route calling another route, sends on the Cookie and Authorization headers of the request being answered, so the API sees the same signed-in person. Give your own headers to send others. A Cookie header you give is read into the route's $cookies, as kscript serve reads it. Cookies a response sets are never carried over to a later request:
<import package="KScript.Net" />
<!-- The route reads $cookies.sb_guest as '0f8f'. -->
<let name="reply" value="@api(/board, options: { headers: { Cookie: 'sb_guest=0f8f' } })" />
<!-- The cookies the response set, such as { ks_session: '...', sb_guest: '...' }. -->
<echo value="$reply.cookies" />
<!-- A list when the response set more than one. -->
<echo value="$reply.headers['Set-Cookie']" />import KScript.Net
// The route reads $cookies.sb_guest as '0f8f'.
$reply = @api(/board, options: { headers: { Cookie: 'sb_guest=0f8f' } })
// The cookies the response set, such as { ks_session: '...', sb_guest: '...' }.
echo $reply.cookies
// A list when the response set more than one.
echo $reply.headers['Set-Cookie']A header sent more than once, such as Set-Cookie, is a list in .headers, and ->header(name) gives the first. .cookies has the name and value of each cookie the response set. Requests inside one another stop after 8, so a route that calls itself is an error rather than a hang.
The Notes example in the fullstack project template works this way: its pages get their notes from /api/notes, so the pages and other clients share one API.