Skip to content

External Libraries in Pre-request or Post-scripts

You can use the following external libraries in your Pre-request or Post-scripts.These libraries provide a wide range of utilities for validation, encoding, cryptography, data manipulation, and more.

Available Built-in Libraries

javascript

// ajv: JSON Schema validator.  
var Ajv = require('ajv');

//atob / btoa: Base64 decoding and encoding utilities.
var atob = require('atob');
var btoa = require('btoa');

//cheerio: Lightweight HTML/XML parsing and manipulation.
var cheerio = require('cheerio');

//crypto-js: Cryptographic algorithms (e.g., MD5, SHA256).
var CryptoJS = require('crypto-js');

//lodash: Utility library for data manipulation (arrays, objects, etc.).
var _ = require('lodash');

//moment: Date parsing, formatting, and manipulation.
var moment = require('moment');

//faker: Random data generation (names, addresses, UUIDs, etc.).
var faker = require('faker');

//tv4: JSON schema validation.
var tv4 = require('tv4');

//uuid: Generation of unique identifiers (UUIDs).
var uuid = require('uuid');

//xml2js: Parsing XML to JS objects and converting JS objects to XML.
var xml2js = require('xml2js');

//path: File and directory path utilities.
var path = require('path');

//assert: Simple assertion library for verifying conditions.
var assert = require('assert');

//buffer: Handling binary data.
var Buffer = require('buffer').Buffer;

//util: Utility functions like formatting and inspection.
var util = require('util');

//url: URL resolution and parsing.
var url = require('url');

//punycode: Convert Unicode strings to ASCII and vice versa.
var punycode = require('punycode');

//querystring: Parse and format URL query strings.
var querystring = require('querystring');

//string-decoder: Decodes buffers to strings, handling multibyte characters.
var StringDecoder = require('string_decoder').StringDecoder;

//stream: Work with streaming data.
var stream = require('stream');

//timers: Scheduling functions like setTimeout and setInterval.
var timers = require('timers');

//events: Event-driven programming with an event emitter.
var events = require('events');

Dynamic CDN Library Loading with requireAsync(url)

You can now dynamically load any JavaScript library from a public CDN using the requireAsync(url) method. This enables you to use popular libraries like Math.js, Lodash, or Moment.js on the fly without bundling or pre-installing anything.

How to Use

Call requireAsync("cdn-url") with the full URL to the desired JavaScript library. It returns a promise that resolves once the script is fully loaded and ready to use.

Example: Using Math.js from CDN

javascript
await requireAsync("https://cdnjs.cloudflare.com/ajax/libs/mathjs/14.2.1/math.js");

var calculation = math.evaluate('1.2 * (2 + 4.5) + 9');
kr.console.log("Calculation", calculation); // Output: 16.8

Notes

  • The library is loaded only once per session, even if you call requireAsync multiple times with the same URL.
  • Libraries are expected to expose themselves on the global object (e.g., math, _, moment).
  • If the URL is invalid or the script fails to load, an error will be thrown.
  • Use trusted CDNs (e.g., jsDelivr, UNPKG, or cdnjs) to avoid CORS or security issues.

Released under the MIT License.