Skip to content

Pre-Request Scripts

Pre-request scripts execute before the request is sent, allowing dynamic variable setup:

JetBrains
< {%
const timestamp = Date.now();
request.variables.set("timestamp", timestamp);
%}

POST https://api.example.com/signed
X-Timestamp: {{timestamp}}
httpyac
{{
const crypto = require('crypto');
exports.signature = crypto.createHmac('sha256', 'secret')
  .update(request.method + request.url)
  .digest('base64');
}}

GET https://api.example.com/resource
X-Signature: {{signature}}
  • request.variables.set/get() — read/write request-scoped variables
  • request.environment.get() — read environment variables
  • request.body.getRaw() — raw request body
  • request.body.tryGetSubstituted() — body with variables resolved
  • request.url.getRaw() — raw URL
  • request.headers.all — all headers
  • request.method — HTTP method

JetBrains exposes a Crypto API: crypto.hmac.sha256(), crypto.sha256(), crypto.sha512(), etc. with chained methods for text/hex/base64 input and output.

In httpyac, double-brace blocks positioned before the request line serve as pre-request scripts. Values exported via exports.variableName become available as variables. The request object is accessible for inspection.

VS Code REST Client and Visual Studio 2022

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

Neither supports pre-request scripts.

Supports JetBrains-compatible pre-request scripts in both JavaScript and Lua.