Skip to content

Pre/Post Scripting

A request can carry two small Rhai scripts. The pre-request script runs before the request is built and can set variables, headers and query parameters. The post-response script runs after the response arrives and can check it with expect() and capture values. Use scripts for things a static .http file cannot express, such as a fresh trace ID per request or a check that compares two fields.

Scripts live in .http files as # @pre-script and # @post-script directive lines above the request line. Each directive holds one line of Rhai.

### Scripted request
# @name scripted
# @pre-script let trace = uuid();
# @pre-script req_set_header("X-Trace-Id", trace);
# @pre-script req_set_query("page", 2);
# @pre-script env_set("course", "CS-" + 401);
# @pre-script log("base_url=" + env_get("base_url"));
# @pre-script log("secret visible? " + (env_get("DEV_API_TOKEN") != ""));
# @post-script expect(res.status == 200, "status is 200");
# @post-script expect(res.json.method == "POST", "echoed method");
# @post-script expect(res.latency_ms < 1000, "under one second");
# @post-script env_set("echo_path", res.json.path);
# @post-script log("content-type=" + res.headers["content-type"]);
POST http://127.0.0.1:4310/echo
Content-Type: application/json
{"course": "{{course}}"}

DEV_API_TOKEN is a secret variable in the demo environment, so the script cannot read it. The target is a local server that echoes the request back.

Terminal window
recon --format human send scripted
POST http://127.0.0.1:4310/echo?page=2 200 1.6ms 321B -> 321B (HTTP/1.1)
✓ script: status is 200
✓ script: echoed method
✓ script: under one second
[script] base_url=http://127.0.0.1:4301/_sandbox/local
[script] secret visible? false
[script] content-type=application/json
{
"method": "POST",
"path": "/echo?page=2",
"headers": {
"host": "127.0.0.1:4310",
"user-agent": "recon/0.3.2",
"accept-encoding": "gzip, br, deflate",
"content-type": "application/json",
"x-trace-id": "d8e9e7bb-16f0-40bb-b8b7-78b12c1ba02b",
"content-length": "20"
},
"body": "{\"course\": \"CS-401\"}"
}

With --format json, the script results appear in three fields:

{
"assertions": [
{ "assertion": "script: status is 200", "detail": "ok", "passed": true },
{ "assertion": "script: echoed method", "detail": "ok", "passed": true },
{ "assertion": "script: under one second", "detail": "ok", "passed": true }
],
"script_logs": [
"base_url=http://127.0.0.1:4301/_sandbox/local",
"secret visible? false",
"content-type=application/json"
],
"captured_vars": {
"echo_path": "/echo?page=2"
}
}
  • # @pre-script <code> adds one line to the pre-request script. # @post-script <code> adds one line to the post-response script. // works as the comment prefix too.
  • The directive is repeatable. Lines are joined with newlines in file order, so multi-line Rhai (loops, functions) works.
  • A bare # @pre-script or # @post-script with nothing after it adds an empty line.
  • Directives must come before the request line. Comment lines after the request line are ignored, including script directives.

In the JSON request model used by the HTTP API and MCP, the same scripts are the pre_script and post_script string fields. In the Web Studio, they are edited on the Scripts tab of the request composer.

Entry point Pre-script Post-script
recon send yes yes
recon test yes, once per data row yes
POST /api/v1/send (Web Studio Send) yes yes
MCP recon_send_request yes yes

Scripts do not run in twin, watch, flow, load, paginate or the protocol commands (sse, ws, graphql, grpc). Those commands ignore the script fields.

For recon send, the order is:

  1. Load the workspace and the selected environment.
  2. Run the pre-request script. Secret references are not resolved yet, and no network call has been made.
  3. Apply the script output to the request: env_set values become variable overrides, req_set_header replaces any header with the same name (case-insensitive), and req_set_query replaces any query parameter with the same name (case-sensitive).
  4. Resolve secrets and {{variables}}, then apply # @auth. Signing therefore covers headers and query parameters added by the script. See Auth & Request Signing.
  5. Check the request against the Outbound Secret Firewall.
  6. Send the request and evaluate # @assert directives.
  7. Run the post-response script. Its expect() results are appended after the # @assert results.
  8. Remove secret values from log lines and captured values, record the run in the Flight Recorder, and print the result.

Both scripts run in a fresh Rhai engine with the Rhai standard library: operators, strings, arrays, maps, loops, functions, type_of(), print and debug. Recon adds the functions below. Nothing else is registered.

Function Returns Description
env_get(name) string Value of a readable variable, or "" if it is missing or secret. See Secrets stay out.
env_set(name, value) Sets a variable. Any value type is converted to a string.
log(value) Adds a line to the script log. Any value type.
print(value) Adds a line to the script log.
debug(value) Adds a line to the script log, in Rhai debug form (strings appear quoted).
uuid() string Random UUID v4.
now_epoch() integer Current Unix time in seconds.
now_iso() string Current UTC time, RFC 3339.
base64_encode(text) string Standard Base64 of the UTF-8 bytes.
sha256_hex(text) string Lowercase hex SHA-256 of the UTF-8 bytes.
# @pre-script print("from print"); debug("from debug"); log(42);
# @pre-script log(now_epoch()); log(now_iso()); log(base64_encode("hi")); log(sha256_hex("hi"));
[script] from print
[script] "from debug"
[script] 42
[script] 1796053767
[script] 2026-11-30T15:49:27.843980575+00:00
[script] aGk=
[script] 8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4
Function Description
req_set_header(name, value) Adds a header. Calling it again with the same name (any case) replaces the earlier value.
req_set_query(name, value) Adds a query parameter. Calling it again with the same name replaces the earlier value.

env_set(name, value) in a pre-script overrides {{name}} for this request. Later env_get(name) calls in the same script see the new value, and so does the post-response script.

There is no request object. A pre-script that reads res fails with Variable not found: res.

Name Description
res Read-only map describing the response.
expect(condition, label) Records an assertion. condition must be a boolean.

res has these fields:

Field Type Content
res.status integer HTTP status code.
res.body string Decoded response body text.
res.json map, array or () Body parsed as JSON, or () when the body is not JSON.
res.headers map Response headers keyed by lowercase name. If a header repeats, the last value wins.
res.latency_ms float Total request time (waterfall.total_ms).

expect(true, "x") records a passing assertion named script: x with detail ok. expect(false, "x") records a failing one with detail script assertion `x` evaluated to false. A failing expect() does not stop the script. req_set_header and req_set_query do not exist here; calling them fails with Function not found.

env_set in a post-script produces captured_vars in the output. Captured values are returned by recon send --format json and POST /api/v1/send. They are not saved to the environment, and recon test does not pass them to later requests.

# @pre-script env_set("run_tag", "nightly");
# @post-script log("tag=" + env_get("run_tag"));
# @post-script log("missing=[" + env_get("nope") + "]");
# @post-script log(type_of(res.json));
GET http://127.0.0.1:4310/gzip 200 0.9ms 50B -> 229B (HTTP/1.1)
[script] tag=nightly
[script] missing=[]
[script] map

env_get only returns plain values. It returns "" for:

  • environment variables defined as secrets (the table form with secret),
  • variables whose value is a secret reference starting with age://, env://, file://, op:// or keychain://,
  • override values (such as recon test --data row values) whose name matches a secret variable or whose value is a secret reference.

The pre-script runs before secret references are resolved, so resolved secret values never reach it.

Log lines and captured_vars are scrubbed of every secret value used in the request before they are printed or returned. The res object is the raw response, not the PII Airlock view.

Limit Value
Operations per script run 50,000
Expression nesting depth 32 (also inside functions)
Function call depth 16
String length 65,536
Array length 1,024
Map size 256 entries
Log lines kept per script 100; extra lines are dropped silently

eval is disabled. Recon registers no file, network or process functions, so a call such as open("/etc/passwd") fails with Function not found.

Message prefix Cause
script compile error: Rhai syntax error, or use of eval.
script runtime error: Error while running, such as an unknown function or variable, or a property of ().
script sandbox limit exceeded: Too many operations, stack overflow, or a string, array or map over its limit.

Real messages from the CLI:

error E_CLI_RUNTIME: script compile error: Unexpected ';' (line 1, position 9)
error E_CLI_RUNTIME: script compile error: reserved keyword 'eval' is disabled (line 1, position 1)
error E_CLI_RUNTIME: script runtime error: Unknown property 'deeper' - a getter is not registered for type '()' (line 1, position 26)
error E_CLI_RUNTIME: script sandbox limit exceeded: Too many operations (line 1, position 6)
error E_CLI_RUNTIME: script sandbox limit exceeded: Stack overflow (line 1, position 22)
error E_CLI_RUNTIME: script sandbox limit exceeded: Length of string too large (line 1, position 33)
error E_CLI_RUNTIME: script sandbox limit exceeded: Size of array/BLOB too large (line 1, position 34)

How each entry point reacts:

Entry point Pre-script error Post-script error Failing expect()
recon send Nothing is sent. Exit code 2. The request was already sent, but the run is not printed or recorded. Exit code 2. Printed with ✗. Exit code 1.
recon test The whole test run stops. Exit code 2. The whole test run stops. Exit code 2. The request counts as failed.
POST /api/v1/send 400 with E_BAD_REQUEST and the script message. 400 with E_BAD_REQUEST. Returned as a failed assertion.
MCP recon_send_request Tool error with the script message. Tool error with the script message. Returned as a failed assertion.
GET http://127.0.0.1:4310/echo 200 0.7ms 169B -> 169B (HTTP/1.1)
✗ script: created (script assertion `created` evaluated to false)
  • Web Studio: the Scripts tab in the request composer edits pre_script and post_script.
  • HTTP API: POST /api/v1/send runs both scripts and returns script_logs and captured_vars. See HTTP API.
  • MCP: recon_send_request runs both scripts. See MCP Tools, Resources & Prompts.