Skip to content

Response Handlers

JetBrains introduced inline JavaScript response handlers placed after the request body using > followed by a script block. External script files can also be referenced: > ./handler.js

JetBrains
GET https://api.example.com/users

> {%
client.test("Status is 200", function() {
  client.assert(response.status === 200);
});
client.global.set("count", response.body.length);
%}
httpyac (JetBrains compat)
GET https://api.example.com/users

> {%
client.test("Status is 200", function() {
  client.assert(response.status === 200);
});
%}
httpyac (native)
GET https://api.example.com/users

{{
const { equal } = require('assert');
test('status is 200', () => {
  equal(response.statusCode, 200);
});
}}
httpyac (assertions)
GET https://api.example.com/users

?? status == 200
?? header content-type includes json
?? duration < 500

client object: client.test(name, fn), client.assert(condition, msg), client.log(text), client.global.set/get/clear/clearAll(), client.global.headers.set(name, value), client.exit().

response object: response.body (parsed JSON, XML DOM, or string), response.status, response.headers.valueOf(name), response.headers.valuesOf(name), response.contentType.mimeType, response.contentType.charset, response.cookiesByName(name).

Scripts use ECMAScript 2023.

httpyac uses double-brace blocks positioned after the request. Scripts have access to response (with .statusCode, .headers, .body, .parsedBody, .timings), full Node.js require(), and built-in test() and sleep() functions.

httpyac also supports the JetBrains response handler syntax for compatibility.

httpyac provides convenience methods for common assertions:

  • test.status(code) — verify HTTP status code
  • test.totalTime(ms) — check request duration
  • test.header(name, value) — assert header value
  • test.headerContains(name, value) — assert header contains substring
  • test.responseBody(content) — assert exact body match
  • test.hasResponseBody() / test.hasNoResponseBody() — check body presence

httpyac provides event-specific hooks: @request (runs after variable replacement, before sending), @streaming (runs during streaming connections), @response (runs on response receipt), @responseLogging (runs on response output), and @after (runs after all requests complete).

Scripts can be registered to execute across all requests in a file using the + prefix. For example, a block with +after runs after every request. A block with just + runs before every request.

httpyac

A compact assertion syntax unique to httpyac:

GET https://api.example.com/users
?? status == 200
?? header content-type includes json
?? duration < 500
?? body $.users[0].name == John
?? js response.parsedBody.total > 0

Supported assertion types: status, header, body, duration, js, xpath.

Operators: ==, !=, >, >=, <, <=, startsWith, endsWith, includes/contains, exists, matches, sha256, sha512, md5.

Supports both JetBrains-style response handler scripts and Lua scripting. Also provides built-in assert functions.

VS Code REST Client and Visual Studio 2022

Section titled “VS Code REST Client and Visual Studio 2022”

Neither supports response handler scripts or assertions. Response data is accessed only through named request variables.

Feature VS CodeJetBrainshttpyacVS 2022kulala
Response handler scripts (> {% %}) httpyac supports for JetBrains compatibility.
Response handler scripts ({{ }}) httpyac-native syntax with full Node.js require() access.
Pre-request scripts (< {% %})
Pre-request scripts ({{ }} before request)
Assertion syntax (?? status == 200) Supports status, header, body, duration, js, xpath assertions.
client.test() API
Crypto API in scripts crypto.hmac.sha256(), crypto.sha256(), etc.
Lua scripting > {% --lua ... %}. Unique to kulala.nvim.
External response handler file (> filepath.js)
Event-specific script hooks {{@request}}, {{@streaming}}, {{@response}}, {{@responseLogging}}, {{@after}}.
Global script registration ({{+...}}) Registers scripts to execute across all requests. {{+after}} runs after every request.
Built-in test helpers (test.status(), test.header()) test.status(), test.totalTime(), test.header(), test.headerContains(), test.responseBody(), etc.