String Interpolation Syntax

String interpolation lets you extract dynamic data at run time from earlier stages of an authentication flow and insert it into later stages. This is needed when a value cannot be hardcoded, for example temporary IDs, CSRF tokens, or access tokens. Each interpolation block is replaced with the actual value every time the authentication flow runs.

An interpolation block is enclosed in double curly braces, {{ and }}, and consists of two parts: a source and, optionally, one or more functions.

📘

Note:

The authentication process sends HTTP requests and receives HTTP responses. In the template syntax, a request-response pair is called a stage.

Format

{{ <source> | <function 1> | <function 2> }}

Text outside the braces is kept as is, and a single value can contain several blocks:

Bearer {{ auth_object.stages.stage1.response.headers | get: '/X-Token' }}

Spaces inside the braces are optional. These two blocks are equivalent:

{{ auth_object.stages.stage1.response.headers | get: '/X-Token' }}
{{auth_object.stages.stage1.response.headers|get:'/X-Token'}}

Stage data

A stage source has four parts, separated by dots:

auth_object.stages.<stage>.<request or response>.<location>
  1. Data context: always auth_object.
  2. Stage: stages. followed by the stage name, for example stages.stage1. Use stages.any when the stage is unknown (see The any stage).
  3. Request/response: request or response.
  4. Location inside the request/response: headers, body, or url.

You can only reference stages that have already run in the current flow.

LocationrequestresponseRules
headersSupportedSupportedThe first function must be get, which selects the header. See Headers.
bodySupportedSupportedget works only on a JSON body. Use match for any other body type. See Body.
urlSupportedNot supportedget is not supported. Use match to extract part of the URL. See URL.
🚧

There is no cookies location

To read a cookie, select the Set-Cookie header and extract the cookie value with match:

{{ auth_object.stages.stage1.response.headers | get: '/Set-Cookie' | match: /sid=(\w+)/ }}

Headers

When the location is headers, the block must start with a get function that names the header. The header name can be written with or without a leading /, and is case-insensitive. These three blocks return the same value:

{{ auth_object.stages.stage1.response.headers | get: '/X-Token' }}
{{ auth_object.stages.stage1.response.headers | get: 'X-Token' }}
{{ auth_object.stages.stage1.response.headers | get: '/x-token' }}

The header name must be a single name. Paths such as /a/b are not valid for headers.

After get, you can add match, encode, and decode. A second get is not allowed.

Body

For a JSON body, use get with the path of the value:

{{ auth_object.stages.stage1.response.body | get: '/authentication/token' }}

For a body in any other format, such as HTML, form data, or plain text, use match:

{{ auth_object.stages.stage1.response.body | match: /name="csrf" value="([^"]+)"/ }}

URL

The URL is available only for requests. It is the URL as configured for that stage, after its own interpolation blocks are resolved:

{{ auth_object.stages.stage1.request.url | match: /session=([^&]+)/ }}

The any stage

If you do not know which stage contains the data, or the stage can change, use any instead of a stage name:

{{ auth_object.stages.any.response.headers | get: '/X-Token' }}

Bright checks the stages that have already run, starting from the most recent one, and uses the first stage for which the whole block succeeds. If the most recent stage does not contain the data, Bright continues to earlier stages. If no stage matches, the authentication flow fails.

🚧

Limitation

any is reserved. Do not use any as the name of a stage.

Supported pipes

Functions (pipes) are separated by the | character and run from left to right. The output of each function is the input of the next one.

A function has a name and may have arguments. Arguments follow the name after a colon (:). Text arguments can be enclosed in single or double quotes.

In this example, get runs first, then match, then encode:

{{ auth_object.stages.stage1.response.headers | get: '/Set-Cookie' | match: /accessToken=([^;]+)/ | encode: 'base64' }}

get

Selects a value:

  • For headers: returns the value of the named header.
  • For body: returns the value at the given path in a JSON body.

Parameters:

  • path (required): the header name, or the path in a JSON body, in quotes.

If the header or path is not found, or the body is not valid JSON, the authentication flow fails.

get is not supported for url or for otpToken.

Examples:

{{ auth_object.stages.stage1.response.headers | get: '/Set-Cookie' }}
{{ auth_object.stages.stage1.response.body | get: '/authentication/token' }}

match

Matches the value against a regular expression and returns a capture group.

Parameters:

  • regex (required): a regular expression enclosed in /, for example /token=(\w+)/. To match a / character, escape it as \/.
  • group (optional, default 1): the number of the capture group to return. Use 0 to return the whole match. The number follows a second colon.

Examples:

{{ auth_object.stages.stage1.response.headers | get: '/Set-Cookie' | match: /sid=(\w+)/ }}
{{ auth_object.stages.stage1.response.headers | get: '/Set-Cookie' | match: /sid=(\w+)/ : 0 }}
{{ auth_object.stages.stage1.response.body | match: /"accessToken":"([^"]+)"/ }}

For a Set-Cookie value of sid=xyz, the first block returns xyz and the second returns sid=xyz.

🚧

Limitations

  • Because the default group is 1, the regular expression must contain a capture group, unless you use group 0.
  • Only group numbers are supported. Named groups cannot be selected.
  • Regular expression flags, such as /i, are not supported. For a case-insensitive match, use character classes, for example /[Tt]oken=(\w+)/.
  • If the regular expression does not match, the authentication flow fails.

encode

Encodes the value.

Parameters:

  • format (optional, default none): base64, url, html, or none.
FormatResult
base64Base64 encoding. abc 123 becomes YWJjIDEyMw==.
urlURL encoding. Spaces become %20. abc 123 becomes abc%20123.
htmlEncodes <, >, &, ", and ' as HTML entities. <a&"b'> becomes &lt;a&amp;&quot;b&#39;&gt;.
noneThe value is not changed.

Example:

{{ auth_object.stages.stage1.response.body | get: '/token' | encode: 'base64' }}

decode

Decodes the value. decode reverses encode.

Parameters:

  • format (optional, default none): base64, url, html, or none.
FormatResult
base64Base64 decoding. aGVsbG8gd29ybGQ= becomes hello world.
urlURL decoding. a%20b%2Fc becomes a b/c.
htmlHTML entity decoding. &lt;b&gt;&amp; becomes <b>&.
noneThe value is not changed.

Example:

{{ auth_object.stages.stage1.response.headers | get: '/X-Encoded-Token' | decode: 'base64' }}

Generating data dynamically

Mock data

To generate random data during authentication, use one of the following Faker.js generators:

GeneratorSyntaxExample output
uuid{{ $faker.datatype.uuid }}cdfcf283-30f1-4ad9-b3c4-564d0950bc70
number{{ $faker.datatype.number }}10503
🚧

Limitations

  • Only uuid and number are supported. Other Faker.js generators are not available.
  • Functions cannot be applied to generated values.

otpToken

Inserts a one-time password (OTP) with preconfigured parameters.

To use an OTP, first configure it. To learn how, see OTP generation settings. Then reference it by the name you gave it:

{{ auth_object.otps.myOtp }}

Here, myOtp is the name of the OTP configuration.

You can apply match, encode, and decode to an OTP. get is not supported.

Troubleshooting

If a block cannot be resolved, the authentication flow fails and the test shows an error.

CauseError
The regular expression did not matchCould not match regex /nomatch=(\w+)/ with group '1'
get was used on a body that is not JSON`get` pipe expects the input to be a valid JSON
A header block does not start with getTemplate syntax error: Expected `get` pipe to be the first one
The header name is a path, such as /a/bTemplate syntax error: Invalid XPath '/a/b' used for header extraction
The URL was read from a responseTemplate syntax error: Can not extract URL from a response
get was used on a URLTemplate syntax error: `get` pipe is not supported for URL
get was used on an OTPTemplate syntax error: `get` pipe is not supported for OTP expression

Did this page help you?