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]
}

contains

The contains operator is used to check whether a given substring or value exists within another string. It returns a boolean (true or false) depending on whether the specified value is found.

This operator is useful in conditional expressions, request matching rules, and transformations, helping you control logic flow based on text patterns or field values.

Syntax:

{{contains stringValue valueToFind}}
  • stringValue – The main string in which you want to search for the substring.
  • valueToFind – The substring you want to check for inside stringValue.

Examples With String:

{
"selected": {{contains "beeceptor-mock-api" "mock"}}
}

Result:

{
"selected": true
}

Note: The search is case-sensitive, so "mock" does not match "Mock".

Examples With Array:

{{contains (array 1 2 3) 1}}

Result:

true

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

slugify

The slugify operator converts a string to lowercase, removes non-word characters and diacritics, and converts spaces to hyphens. Also strips leading and trailing whitespace.

Syntax:

{{slugify string}}
ParameterRequiredDescription
stringYesThe non-slugified string containing non-word characters and diacritics

Example:

{
slug_value: "{{slugify 'some product name'}}"
}

Result:

{
slug_value: "some-product-name"
}

stripTags

The stripTags operator strips all [X]HTML/XML tags from a string.

Syntax:

{{stripTags 'string'}}
ParameterRequiredDescription
stringYesThe string containing [X]HTML/XML tags

Example:

{
tagValue: "{{stripTags '<greeting>Hello</greeting>'}}"
}

Result:

{
tagValue: "Hello"
}

replace

The replace operator replaces all occurrences of the specified substring with a replacement value.

Syntax:

{{replace string findMe replaceWith }}
ParameterRequiredDescription
stringYesThe string to be modified
findMeYesThe substring to be replaced in the base string
replaceWithYesThe replacement value for the substring

Example:

{
value: "{{replace 'the wrong turn' 'wrong' 'right' }}"
}

Result:

{
value: "The right turn"
}

trim

The trim operator removes whitespaces from the start and end of the input string.

Syntax:

{{trim string}}
ParameterRequiredDescription
stringYesThe input string containing whitespaces from its start and end

Example:

{
trimVal: "{{trim ' How are you doing? '}}"
}

Result:

{
trimVal: "How are you doing?"
}