Skip to content

Response

Available in post-request scripts only. Not defined during pre-request scripts.

Read-only access to the HTTP response from the send that just completed. For Postman-style assertions on status, headers, and body, use hc.response.to inside hc.test.

hc.response.code

Available since v2.0.0

Signature:number (read-only)

HTTP status code (for example 200, 404). Maps to the response status field.

javascript
hc.expect(hc.response.code).to.equal(200);

hc.response.document()

Available since v2.0.0

Signature:() => { querySelector, querySelectorAll }

ParameterTypeDescription
querySelector(selector)element or `nullFirst match for a CSS selector
querySelectorAll(selector)array of elementsEvery match for a CSS selector

Parses the response body as HTML and returns a document object for CSS selector queries. The parser is Cheerio-backed; this is not a full browser DOM.

Availability: post-request scripts only.

The first call in a script parses the body; later calls reuse the same parsed document.

Document methods:

Element properties:

Property / method Description
textContent Concatenated text of the element and its descendants
getAttribute(name) Attribute value, or null when absent
innerHTML Serialized inner HTML of the element's children

Limitations:

  • CSS selectors only (no XPath)
  • No script execution and no network fetches during parse
  • Parses whatever body string the response contains; does not check Content-Type
  • Not a browser Document — elements have no nested querySelector, and there is no Cheerio $() API
javascript
var doc = hc.response.document();

hc.test("page has login form", function () {
  var form = doc.querySelector("form#login");
  hc.expect(form).to.be.ok;
  hc.expect(form.getAttribute("method")).to.equal("post");
});

hc.test("lists three items", function () {
  hc.expect(doc.querySelectorAll("ul.items li").length).to.equal(3);
});

hc.response.headers

Available since v2.0.0

Signature:Record<string, string> (read-only)

Response headers as a flat key-value map.

javascript
var contentType = hc.response.headers["content-type"];

hc.response.json()

Available since v2.0.0

Signature:() => unknown

Parses the response body as JSON and returns the result. Throws if the body is not valid JSON.

javascript
var data = hc.response.json();
hc.expect(data.id).to.be.ok;

hc.response.responseTime

Available since v2.0.0

Signature:number (read-only)

Round-trip time for the request in milliseconds.

javascript
hc.test("responds quickly", function () {
  hc.expect(hc.response.responseTime < 1000).to.be.ok;
});

hc.response.status

Available since v2.0.0

Signature:string (read-only)

HTTP status text (for example OK, Not Found).

javascript
console.log(hc.response.status);

hc.response.text()

Available since v2.0.0

Signature:() => string

Returns the response body as a string.

javascript
var body = hc.response.text();

hc.response.to

Available since v2.0.0

ParameterTypeDescription
jsonContent-Type includes JSON and body parses
withBodyBody is non-empty
ok` / `successStatus is 2xx
redirectionStatus is 3xx
clientErrorStatus is 4xx
serverErrorStatus is 5xx
errorStatus is 4xx or 5xx
acceptedStatus is 202
badRequestStatus is 400
unauthorizedStatus is 401
forbiddenStatus is 403
notFoundStatus is 404
rateLimitedStatus is 429

Postman-style response assertions powered by Chai. Use inside hc.test:

Methods on hc.response.to.have:

Matcher Description
status(code) Assert HTTP status code (number)
status(text) Assert status text (string, case-insensitive)
header(name) Assert header is present
header(name, value) Assert header value
body() Assert body is non-empty
body(text) Assert exact body text
body(/regex/) Assert body matches regex
jsonBody() Assert body parses as JSON
jsonBody(object) Assert parsed JSON deeply equals object

Properties on hc.response.to.be:

Not supported on hc.response.to: jsonSchema (requires ajv), JSON-path form jsonBody(path, value). For response time, use hc.expect(hc.response.responseTime).to.be.below(ms).

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

hc.test("status text", function () {
  hc.response.to.have.status("OK");
});

hc.test("not found", function () {
  hc.response.to.not.have.status(404);
});

hc.test("returns JSON", function () {
  hc.response.to.be.json;
});

hc.test("body shape", function () {
  hc.response.to.have.jsonBody({ ok: true });
});