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:
- By default, template engine is off. You need to explicitly mark a rule to enable usage of operators and If/Else constructs.
- 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:
Result: hello world
uppercase
The uppercase
operator converts a string to uppercase.
Example:
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:
Parameter | Required | Default | Description |
---|---|---|---|
string | Yes | - | The string to pad |
length | Yes | - | Target length of the resulting string |
padChar | No | " " (space) | Character to pad with |
Example:
Result: 00042
padEnd
The padEnd
operator pads the end of a string with a specified character until it reaches the desired length.
Syntax:
Parameter | Required | Default | Description |
---|---|---|---|
string | Yes | - | The string to pad |
length | Yes | - | Target length of the resulting string |
padChar | No | " " (space) | Character to pad with |
Example:
Result: 42000
split
The split
operator divides a string into an array of substrings based on a specified separator.
Syntax:
Parameter | Required | Default | Description |
---|---|---|---|
string | Yes | - | The string to split |
separator | No | " " (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:
or as a block helper:
Parameter | Required | Description |
---|---|---|
string | Yes (for non-block usage) | The string to encode to Base64 |
Examples:
- Encode a simple string:
Result: aGVsbG8gd29ybGQ=
- Using the block helper form:
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:
or as a block helper:
Parameter | Required | Description |
---|---|---|
encoded_string | Yes (for non-block usage) | The Base64-encoded string to decode |
Examples:
- Decode a Base64 string:
Result: hello world
- Using the block helper form:
Result: username:password