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.
hc.expect(hc.response.code).to.equal(200);hc.response.document()
Available since v2.0.0
Signature:() => { querySelector, querySelectorAll }
| Parameter | Type | Description |
|---|---|---|
querySelector(selector) | element or `null | First match for a CSS selector |
querySelectorAll(selector) | array of elements | Every 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 nestedquerySelector, and there is no Cheerio$()API
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.
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.
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.
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).
console.log(hc.response.status);hc.response.text()
Available since v2.0.0
Signature:() => string
Returns the response body as a string.
var body = hc.response.text();hc.response.to
Available since v2.0.0
| Parameter | Type | Description |
|---|---|---|
json | | Content-Type includes JSON and body parses |
withBody | | Body is non-empty |
ok` / `success | | Status is 2xx |
redirection | | Status is 3xx |
clientError | | Status is 4xx |
serverError | | Status is 5xx |
error | | Status is 4xx or 5xx |
accepted | | Status is 202 |
badRequest | | Status is 400 |
unauthorized | | Status is 401 |
forbidden | | Status is 403 |
notFound | | Status is 404 |
rateLimited | | Status 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).
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 });
});