Skip to content

Examples

Pre-request: set URL, header, and token

Runs before the request is sent. Sets the target URL, attaches an auth header, and stores a token for {{token}} substitution elsewhere.

javascript
hc.request.url = "https://api.example.com/v1/users";
hc.request.method = "GET";
hc.request.variables.set("token", "abc123");
hc.request.headers.set(
  "Authorization",
  "Bearer " + hc.request.variables.get("token")
);
console.log("pre-request ran");

Post-request: assert status and JSON body

Runs after the response is received. Registers tests that appear in the Tests tab.

javascript
hc.test("status is 200", function () {
  hc.expect(hc.response.code).to.equal(200);
});

hc.test("body has ok", function () {
  hc.expect(hc.response.json()).to.eql({ ok: true });
});

Post-request: assert HTML response

Runs after an HTML response is received. Uses hc.response.document() to query the body without regex or string parsing.

javascript
hc.test("status is 200", function () {
  hc.expect(hc.response.code).to.equal(200);
});

var doc = hc.response.document();

hc.test("page title", function () {
  hc.expect(doc.querySelector("h1")?.textContent).to.equal("Welcome");
});

hc.test("item count", function () {
  hc.expect(doc.querySelectorAll(".item").length).to.equal(3);
});

Chain variables across collection and request scripts

Use separate inline scripts at each level. The collection script runs first and sets a variable; the request script reads it when building the URL. For objects, arrays, or mock fixtures, prefer hc.data instead of serializing JSON into a variable string.

Collection pre-request script (first script in the collection PreRequest list):

javascript
hc.request.variables.set("baseUrl", "https://api.example.com");

Request pre-request script (first script in the request PreRequest list):

javascript
hc.request.url = hc.request.variables.get("baseUrl") + "/v1/status";

The request URL becomes https://api.example.com/v1/status when sent.

Share mock data across scripts

Use hc.data when one script defines structured state and another script in the same send consumes it. The bag carries from pre-request scripts through post-request scripts for that send.

Collection or request pre-request script (define mocks):

javascript
hc.data.mocks = {
  user: { id: 123, name: "Ada" },
  items: ["alpha", "beta"],
};

Request or collection post-request script (use mocks in tests):

javascript
const user = hc.data.mocks.user;
hc.test("mock user id", function () {
  hc.expect(user.id).to.equal(123);
});
hc.test("mock item count", function () {
  hc.expect(hc.data.mocks.items.length).to.equal(2);
});

Pre-request: fetch a token with hc.fetch

Requires Allow script network requests in Settings → General.

javascript
var res = await hc.fetch("https://api.example.com/oauth/token", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ grant_type: "client_credentials" }),
});

var data = await res.json();
hc.request.variables.set("token", data.access_token);
hc.request.auth.set({
  type: "bearer",
  token: hc.request.variables.get("token"),
});

Pre-request: mock a response with hc.sendJSON

Use hc.sendJSON (or hc.send) to supply a synthetic response, then hc.execution.skipRequest() so the remote server is never contacted. Post-request scripts do not run when the request is skipped.

javascript
await hc.sendJSON(
  {
    error: "Something happened.",
  },
  400
);
hc.execution.skipRequest();

In a post-request script, the same APIs discard the real response after the script phase finishes and show the override in the response viewer instead. History still records the real response when a send occurred.