>_ RESTERM
v1.18.2

examples Scripts and checks

javascript.http

JavaScript pre-request and test scripts, and a test that saves a response body to a file.

runs as-is

shell
resterm run --all _examples/javascript.http

Run this from the root of a clone of the Resterm repository.

Editor
# JavaScript still works, but RestermScript is the first choice for scripts.
# Use JavaScript for scripts you already have, or for the few things
# RestermScript does not do, such as saving a response body to a file.
# scripts.http sets up the same token with RestermScript.
#
# Put JavaScript under `# @script pre-request` or `# @script test` and start
# each line with `>`. A `> < ./js/save-body.js` line runs that file as its own script.

### Bootstrap Session With JavaScript
# @name JavaScriptBootstrap
# @tag scripts javascript
# @description Pre-request script seeds and reuses a global token while setting headers and body.
# @script pre-request
> var existing = vars.global.get("js.token");
> var token = existing || `js-${Date.now()}`;
> vars.global.set("js.token", token, {secret: true});
> request.setHeader("Authorization", `Bearer ${token}`);
> request.setHeader("X-Trace-ID", `trace-${Date.now()}`);
> var payload = {
>   correlationId: `corr-${Date.now()}`,
>   scope: "reports",
>   requestedAt: new Date().toISOString()
> };
> request.setBody(JSON.stringify(payload, null, 2));
# @script test
> var echoed = response.json().headers.Authorization;
> tests.assert(echoed === `Bearer ${vars.global.get("js.token")}`, "Authorization should carry the global token");
POST {{services.api.base}}/reports/sessions
Content-Type: application/json
Accept: application/json

### Save A Downloaded Image
# @name JavaScriptSaveImage
# @tag scripts javascript
# @description Runs js/save-body.js, which saves the image as resterm-example.png.
# @description The image is written to the directory Resterm was started in, not next to this file.
# @script test
> < ./js/save-body.js
GET {{base.url}}/image/png
Accept: image/png

_examples/javascript.http on GitHub

JavaScript hooks docs
Search

↑↓ moveEnter openEsc close

Help

These keys work like the ones in the TUI.

/
Search the docs (also Ctrl K)
j k
Scroll down and up
g g G
Jump to the top or the bottom
[ ]
Previous and next page
g d
Docs index
g h
Home page
t
Switch between dark and light
?
Show this help
Esc
Close a dialog