Scripting - amitsri/rostyman-releases GitHub Wiki

Scripting

Rostyman supports pre-request and post-response scripts using JavaScript. Scripts use the rm.* API (similar to the scripting API in other API clients).

Script Types

  • Pre-request — runs before the request is sent
  • Post-response — runs after the response is received

rm.* API

Variables

// Environment variables
rm.environment.get("key")
rm.environment.set("key", "value")
rm.environment.has("key")
rm.environment.unset("key")

// Collection variables
rm.collectionVariables.get("key")
rm.collectionVariables.set("key", "value")

// Global variables
rm.globals.get("key")
rm.globals.set("key", "value")

// Any variable (reads from env → collection → globals)
rm.variables.get("key")

Request (Pre-request only)

rm.request.method   // "GET", "POST", etc.
rm.request.url      // full URL
rm.request.headers  // header object
rm.request.body     // request body string

Response (Post-response only)

rm.response.code         // status code (200, 401, etc.)
rm.response.status       // status text ("OK", "Unauthorized")
rm.response.responseTime // response time in ms
rm.response.json()       // parse response body as JSON
rm.response.text()       // response body as text
rm.response.headers.get("Content-Type")
rm.response.size.total   // body size in bytes

Tests

rm.test("Status code is 200", function () {
    rm.expect(rm.response.code).to.equal(200);
});

rm.test("Response is JSON", function () {
    rm.response.json();
});

rm.test("Body contains key", function () {
    const body = rm.response.text();
    rm.expect(body).to.include("success");
});

rm.test("Response time < 500ms", function () {
    rm.expect(rm.response.responseTime).to.be.below(500);
});

Assertions

rm.expect(value).to.equal(expected)
rm.expect(value).to.eql(expected)          // deep equal
rm.expect(value).to.include(substring)
rm.expect(value).to.be.a("string")
rm.expect(value).to.be.above(n)
rm.expect(value).to.be.below(n)
rm.expect(value).to.be.ok                  // truthy
rm.expect(value).to.be.true
rm.expect(value).to.be.false
rm.expect(value).to.be.null
rm.expect(value).to.exist
rm.expect(value).to.have.property("key")
rm.expect(value).to.have.lengthOf(n)
rm.expect(value).not.to.equal(expected)
rm.expect(value).not.to.include(substring)

Script Execution Order

When a request is inside folders and a collection, scripts execute in this order:

  1. Collection pre-request script
  2. Root folder pre-request script
  3. Sub-folder pre-request scripts (outermost → innermost)
  4. Request pre-request script
  5. Request is sent
  6. Request post-response script
  7. Sub-folder post-response scripts (innermost → outermost)
  8. Root folder post-response script
  9. Collection post-response script

Variables set by rm.environment.set(), rm.collectionVariables.set(), or rm.globals.set() in earlier scripts are available in later scripts in the chain.

Where to Add Scripts

  • Collection scripts — click collection name in sidebar → Scripts tab
  • Folder scripts — click folder name in sidebar → Scripts tab
  • Request scripts — in the request builder → Scripts tab (Pre-request / Post-response)

Snippets

Click Snippets at the bottom of the Scripts tab for categorized code templates:

  • Tests — status code, body check, JSON value, response time
  • Variables — set/get environment, collection, global variables
  • Logging — console.log statements

Beautify

Click Beautify to auto-format your JavaScript code.

Variable Persistence

Variables set in scripts persist to the database immediately:

  • rm.environment.set() → updates the active environment
  • rm.collectionVariables.set() → updates collection variables
  • rm.globals.set() → updates global variables

Changes are reflected in the UI Variables panel instantly.

AI-Assisted Scripting

The AI Assistant can help you write and debug scripts:

  1. Open the AI panel (Ctrl+Alt+P)
  2. Click Tests quick action — the AI generates rm.test() scripts from your current response
  3. Code blocks in the AI response have an "Apply as Test Script" button — click to insert directly
  4. Ask the AI to explain, fix, or improve your scripts in natural language
  5. The AI understands Rostyman's rm.* API and generates compatible code