Skip to main content

String Operators

You can use comparison operators within an if/else block to create dynamic responses based on incoming request parameters. In Beeceptor, comparison expressions follow a prefix-based notation, where the operator is placed before the arguments. Refer to the supported operators and their usage to craft dynamic mock responses.

Note:

  1. By default, template engine is off. You need to explicitly mark a rule to enable usage of operators and If/Else constructs.
  2. If you have a use-case for an operator that isn't listed below, please contact our Support Team. They can assist you in prioritizing your request.

lowercase

The lowercase operator converts a string to lowercase.

Example:

{{lowercase "HELLO WORLD"}}

Result: hello world

uppercase

The uppercase operator converts a string to uppercase.

Example:

{{uppercase "hello world"}}

Result: HELLO WORLD

concat

The concat operator combines multiple strings or arrays together.

Syntax:

{{concat value1 value2 value3 ...}}

Examples:

{
"string-concat": "{{concat "Hello" " " "World"}}",
"array-concat": [{{concat (array 1 2) (array 3 4)}}]
}

Result:

{
"string-concat": "Hello World",
"array-concat": [1,2,3,4]
}

padStart

The padStart operator pads the beginning of a string with a specified character until it reaches the desired length.

Syntax:

{{padStart string length [padChar]}}
ParameterRequiredDefaultDescription
stringYes-The string to pad
lengthYes-Target length of the resulting string
padCharNo" " (space)Character to pad with

Example:

{{padStart "42" 5 "0"}}

Result: 00042

padEnd

The padEnd operator pads the end of a string with a specified character until it reaches the desired length.

Syntax:

{{padEnd string length [padChar]}}
ParameterRequiredDefaultDescription
stringYes-The string to pad
lengthYes-Target length of the resulting string
padCharNo" " (space)Character to pad with

Example:

{{padEnd "42" 5 "0"}}

Result: 42000

split

The split operator divides a string into an array of substrings based on a specified separator.

Syntax:

{{split string [separator]}}
ParameterRequiredDefaultDescription
stringYes-The string to split
separatorNo" " (space)Character to use as separator

Example:

{
"num-split": [{{split "1;2;3;4" ";"}}],
"str-split": [
{{#each (split 'part1-part2-part3' '-')}}
"{{this}}"{{#unless @last}},{{/unless}}
{{/each}}
]
}

Result:

{
"num-split": [1,2,3,4],
"str-split": ["part1", "part2", "part3"]
}

len

The len operator in Beeceptor calculates the length of arrays, strings, or numbers from the request payload. It is helpful for creating mock responses that needs to emit the size of input data.

Syntax

{{len (body 'parameterName')}}
  • In the above, body refers to the request payload helper.
  • The parameterName specifies the key to evaluate from the request payload/body.

Other examples:

  • {{len (faker 'string.uuid')}}: Returns 36 as the length of UUIDs.
  • {{len 'Alex'}}: Returns 4 as the size of given string.

Example

Request Payload:

{
"companyName": "ACME Corporation",
"amount": 1234567,
"listOfItems": [
{"name": "one", "description": "details about one"},
{"name": "two", "description": "details about two"},
{"name": "three", "description": "details about three"}
]
}

Mock Template:

{
"itemsCount": {{len (body 'listOfItems')}},
"companyNameLength": {{len (body 'companyName')}},
"amountSize": {{len (body 'amount')}}
}

Response Generated:

{
"itemsCount": 3,
"companyNameLength": 16,
"amountSize": 7
}

base64

The base64 operator encodes a string to Base64 format. This is useful when you need to generate Base64-encoded content for authentication headers, data URLs, or other scenarios requiring encoded data.

Syntax:

{{base64 string}}

or as a block helper:

{{#base64}}content{{/base64}}
ParameterRequiredDescription
stringYes (for non-block usage)The string to encode to Base64

Examples:

  1. Encode a simple string:
{{base64 "hello world"}}

Result: aGVsbG8gd29ybGQ=

  1. Using the block helper form:
{{#base64}}
username:password
{{/base64}}

Result: dXNlcm5hbWU6cGFzc3dvcmQ=

base64Decode

The base64Decode operator decodes a Base64-encoded string back to its original form. This is helpful when you need to extract information from Base64-encoded content in request headers or payloads.

Syntax:

{{base64Decode encoded_string}}

or as a block helper:

{{#base64Decode}}encoded_content{{/base64Decode}}
ParameterRequiredDescription
encoded_stringYes (for non-block usage)The Base64-encoded string to decode

Examples:

  1. Decode a Base64 string:
{{base64Decode "aGVsbG8gd29ybGQ="}}

Result: hello world

  1. Using the block helper form:
{{#base64Decode}}
dXNlcm5hbWU6cGFzc3dvcmQ=
{{/base64Decode}}

Result: username:password