JetClient Library

The JetClient Library provides a comprehensive JavaScript API for interacting with your API requests, responses, and writing tests. Most functionality is accessed through the global jc object and some utility functions are defined globally.

To see complete JetClient Library type definitions, write jc in script and press Cmd + B or Cmd + mouse click to navigate to the type definition.

The jc Object

The jc object is your main interface to JetClient’s functionality, providing access to:

  • Request and response data
  • Variable management across different scopes
  • Test assertions and validations
  • Request execution control
  • File operations
  • Cookie management

Variables

JetClient supports multiple variable scopes including runtime, global, environment, and folder variables. For detailed information about working with variables in scripts, see the Variables section.

Testing and Assertions

JetClient provides comprehensive testing capabilities through the jc.test function and Chai.js assertions. For detailed information about writing tests, see the Write Tests section.

Global Utilities

Console Methods

The console object provides logging capabilities:

console.log("Info message", data)
console.warn("Warning message")
console.error("Error message")
console.info("Info message")
console.time("label")    // Start timer
console.timeEnd("label") // End timer and log duration

Require Function

Import built-in or custom libraries:

const moment = require('moment')
const myLibrary = require('/path/to/myLibrary.js')

For information about available libraries and how to use them, see the Built-in Libraries and External Libraries sections.

SetTimeout and ClearTimeout

Control execution timing using:

// Execute function after delay (milliseconds)
const timeoutId = setTimeout(function() {
    console.log("Delayed execution")
}, 1000)

// Cancel scheduled execution
clearTimeout(timeoutId)

Request and Response Handling

Working with Requests

Access and modify the current request through jc.request:

// Request properties
jc.request.method    // HTTP method
jc.request.url       // Request URL
jc.request.headers   // Request headers
jc.request.body      // Request body

// Modify request
jc.request.setMethod("POST")
jc.request.setUrl("https://api.example.com")
jc.request.setHeader("Content-Type", "application/json")
jc.request.setQueryParam("limit", 10)
jc.request.setBodyJson({ key: "value" })

// Authentication
jc.request.setAuthBasic("username", "password")
jc.request.setAuthBearer("token")
jc.request.setAuthApiKey("key", "value", true) // true for header, false for query
jc.request.setNoAuth()

Working with Responses

Access response data through jc.response:

// Response properties
const statusCode = jc.response.code        // HTTP status code
const statusText = jc.response.status      // Status text
const headers = jc.response.headers        // Response headers
const responseTime = jc.response.time      // Response time in ms
const responseSize = jc.response.size      // Response size in bytes

// Response body
const text = jc.response.text()           // Get response as text
const json = jc.response.json()           // Parse response as JSON
const jsonValue = jc.response.json("$.path") // Get JSON value using JSONPath

Managing Cookies

The jc.cookies object provides methods for working with cookies.

// Get a cookie value
const sessionToken = jc.cookies.get("session");

// Get all cookies
const allCookies = jc.cookies.getAll();

// Set a cookie
jc.cookies.set("user", "JohnDoe");

// Delete a cookie
jc.cookies.unset("session");

// Clear all cookies for a specific URL
jc.cookies.clear("https://example.com");

// Clear all cookies for all URLs
jc.cookies.clearAll();

Sending Requests

Send requests using sendRequest or runRequest. runRequest executes the same way as sendRequest but additionally runs associated pre-request and post-response scripts:

// Using HttpRequest builder
const response = await jc.sendRequest(new HttpRequest()
    .setUrl("https://api.example.com/data")
    .setMethod("GET")
    .setHeader("Authorization", "Bearer token"))

// Using object literal
const response = await jc.sendRequest({
    url: "https://api.example.com/data",
    method: "GET",
    headers: { "Authorization": "Bearer token" }
})

// Using promise chain
jc.sendRequestAsync("/folder/requestName")
    .then(response => {
        console.log(response.json())
    })
    .catch(error => {
        console.error("Request failed:", error)
    })

// Using existing request with modifications
await jc.sendRequestAsync("/folder/requestName", (req) => {
    req.setQueryParam("id", "123")
    return req
})

Running Folders and Scripts

// Run all requests in a folder
jc.runFolder("/api-tests")
await jc.runFolderAsync("/api-tests")

// Run a script and get its return value
const result = jc.runScript("/scripts/calculate")
const result = await jc.runScriptAsync("/scripts/calculate")

Execution Control

Control request execution flow:

// In collection runner or folder runs
jc.execution.setNextRequest("/folder/nextRequest") // Set next request
jc.execution.setNextRequest(null)                 // Stop execution

// In pre-request scripts
jc.execution.skipRequest() // Skip current request

File Operations

Reading Files

// Read file content
const content = jc.readFile("data.json")
const content = jc.readFile("image.png", "base64")