Scripting
It is possible to use NodeJS scripts. All scripts before the request line are executed before the request is called. All scripts after the request line are executed as soon as the response is received. All exports of the script are stored as variables.
{{
//pre request script
const crypto = require('crypto');
const date = new Date();
const signatureBase64 = crypto.createHmac('sha256', 'secret')
.update(`${request.method}\u2028${request.url}\u2028${date.getTime()}`).digest("base64");
exports.authDate = date.toUTCString();
exports.authentcation = `Basic ${signatureBase64}`;
}}
GET https://httpbin.org/anything
Date: {{authDate}}
Authentication: {{authentcation}}
{{
// post request script
const assert = require('assert');
test("authentcation is valid", () => {
assert.equal(response.parsedBody.headers.Authentication, authentcation);
});
}}
WARNING
Scripts are executed in a custom context/ execution environment. This context should behave identically to NodeJS Default execution environment, but there may be variations. These can be bypassed using require.
WARNING
The line break after '{{'
is important to distinguish between script execution and variable substitution/ templating.
Async/Await or Promise
If the execution of the script is async
, it is necessary to export this Promise. In this case, the program waits for the Promise to be completed.
{{
async function wait(){
const date = new Date();
await sleep(10000);
const afterDate = new Date();
return afterDate.getTime() - date.getTime();
}
exports.wait = wait();
}}
GET https://httpbin.org/anything?delay={{wait}}
Access to Variables
All Variables already defined can be accessed via the global object.
WARNING
Since all variables are placed on the global scope of the script, they may overwrite other variables. Please use unique variable names
In addition to the defined variables and NodeJS Global, the following values are also set on global object.
name | description | condition | example |
---|---|---|---|
$global | Object which allows storing global Variables | globalVariables.http | |
$requestClient | requestClient to send additional body in streaming event. | requestClient.http | |
httpFile | current httpFile | - | scriptVariablesHttpFile.http |
httpRegion | current httpRegion | - | scriptVariablesHttpRegion.http |
oauth2Session | OAuth2 Response | only if OAuth2/ OpenId Connect is used | scriptVariablesOAuthSession.http |
request | request of the next http request | - | custom.http |
response | http response of the last executed request | only use it in post request scripts or for responses imported with @forceRef | websocket.http |
sleep | Method to wait for a fixed period of time | - | grpcClientStreaming.http |
test | method to simplify tests (assert or chai) | - | test.http |
__dirname | path to current working directory | - | - |
__filename | The file name of the current module. | - | - |
Require
External scripts can be imported using require, but you need to install dependencies yourself.
{{
const { authenticate } = require('./scriptRequire.cjs');
const authDate = new Date();
exports.authDate = authDate.toUTCString();
exports.authentication = authenticate(authDate, request);
}}
GET https://httpbin.org/anything
Authentication: {{authentication}}
Date: {{authDate}}
TIP
External dependencies must be installed independently, exceptions are , , and Dependency, which are provided from the extension.
External dependencies must be installed independently, but some dependencies are provided from httpyac:
- @cloudamqp/amqp-client
- @xmldom/xmldom
- dayjs (example)
- eventsource
- got
- grpc-js (example)
- httpYac
- mqtt
- NodeJS API (example)
- uuid (example)
- vscode
- ws
- xpath (example)
WARNING
NodeJS caches all loaded scripts. Since in VS Code the script is executed in the context of the extension, the content of the script is not reloaded. Therefore, the script must be manually removed from the cache.
Console
The console cannot be accessed in VS Code. Therefore, a separate Console object is provided in the context of the script that redirects the output to the OutputChannel.
@foo = https://httpbin.org
{{
console.info(foo);
}}
Open the OutputChannel by pressing CTRL+K
followed by CTRL+H
, then select httpyac - Console
from the select on the right.
Global Scripts
Scripts with no request in the same region are always executed (Global Scripts).
{{
console.info('on every run')
}}
###
GET https://httpbin.org/json
GET https://httpbin.org/json
Events
The normal script registration is executed in order of occurrence in http File. For the execution of a request, several steps have to be executed besides the actual request: Replace Variables, Prepare Body, Pretty Print Body. With events scripts can be hooked into concrete steps in this process.
{{@response
console.info("on every request");
}}
GET https://httpbin.org/json
The following events are possible.
Events | Description |
---|---|
request | event triggered before every request (but after replaceVariableHook) |
streaming | event triggered while client streaming |
response | event triggered on response of request |
responseLogging | event triggered on output of response, used for altering output which is provided with variable response |
after | event triggered after all request is sent |
TIP
The events can be registered automatically globally using +
.
{{+after
console.info(`on every request: ${response.statusCode}`);
}}
###
GET https://httpbin.org/json
GET https://httpbin.org/json
If no event is specified for global registration, the script is executed before every request.
{{+
console.info("on every request");
}}
###
GET https://httpbin.org/json
GET https://httpbin.org/json
Dynamic canceling current execution
It is possible to interrupt the execution of the current HttpRegion directly. If a script exports a variable $cancel
, the further execution of the current HttpRegion is canceled.
{{
exports.$cancel = true;
}}
GET https://httpbin.org/json
Intellij Script
Intellij Scripts are supported. An Http client and response object corresponding to the interface is created and are available in the script.
WARNING
The execution environment differs between NodeJS and Intellij (uses Nashorn). Possibly the behavior is not completely identical, to Intellij Execution. If there are problems, please let me know.
POST https://httpbin.org/anything
Content-Type: application/x-www-form-urlencoded
accept: application/json
email=user@domain.loc&password=2
> ./intellij.js
POST https://httpbin.org/anything
Content-Type: application/x-www-form-urlencoded
accept: application/json
email=user@domain.loc&password=3
> {% client.global.set("email", response.body.form.email); %}
How to debug scripts
- Install httpYac cli with
npm install httpyac -g
- open Http File in VS Code
- add debugger; statement in script
- open Javascript Debug Terminal in VS Code
- execute command
httpyac <file> -l <line>
{{
//pre request script
const crypto = require('crypto');
const date = new Date();
const signatureBase64 = crypto.createHmac('sha256', 'secret')
.update(`${request.method}\u2028${request.url}\u2028${date.getTime()}`).digest("base64");
debugger;
exports.authDate = date.toUTCString();
exports.authentcation = `Basic ${signatureBase64}`;
}}
GET https://httpbin.org/json
Date: {{authDate}}
Authentication: {{authentcation}}