Comparison & Arithmetic 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.
Comparison Operators
eq (equals compare)
The eq
(equals) operator checks if two arguments are equal, returning a boolean value. It supports both number and string data types and returns false if the types or the values don't match.
The following example demonstrates how to use the eq
operator to return a specific response when both arguments are the same.
{{#if (eq 100 100)}}
This text block is always qualified for the response.
{{/if}}
You can also use the eq
operator to conditionally send a response based on the value of a field in the request body. In the example below, the response block varies based on the value of stage
field in the request body.
{{#if (eq (body 'stage') 1)}}
{
"message": "This JSON is qualified for the response if the stage field is sent as 1."
}
{{else}}
{
"message": "This JSON is sent if the stage field is not 1."
}
{{/if}}
Explore more operators and their usage to craft flexible and dynamic mock responses that fit your specific needs.
gt (greater than)
The gt
(greater than) operator checks if the first argument is greater than the second, returning a boolean value.
The example below shows how to use the gt
operator to send a conditional response based on the creditScore
field in the request body. If the creditScore
exceeds 700, the application status is marked as "APPROVED".
{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (gt (body 'creditScore') 700 )}}
"status": "APPROVED",
{{else}}
"status": "REJECTED",
{{/if}}
}
lt (less than)
The lt
(less than) operator checks if the first argument is lesser than the second, returning a boolean value.
The example below shows how to use the lt
operator to send a conditional response based on the creditScore
field in the request body. If the creditScore
is less than 700, the application status is marked as "REJECTED."
{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (lt (body 'creditScore') 700)}}
"status": "REJECTED",
{{else}}
"status": "APPROVED",
{{/if}}
}
Arithmetic Operations
Beeceptor supports basic arithmetic operations in response template. These operations allow you to perform calculations using numbers from request parameters or static values, enabling dynamic HTTP response for numerical data.
add
The add
operator sums a list of numbers, which can be a combination of request parameters and static values.
Example:
The following template picks price
and tax
values from the request payload (JSON) and computes total
in the HTTP response:
{
"total": {{add (body 'price') (body 'tax') 5}}
}
When the request body is {"price": 100, "tax": 10}
, the response will be:
{
"total": 115
}
Additional Examples:
{{add 10 5 2}}
: Adds a static list of numbers (10, 5, and 2).{{add (body 'price') 2}}
: Combines dynamic values (e.g., from the request body) with static values in a single operation.{{add '10' 5.4 (body 'price')}}
: Demonstrates how you can mix data types (strings, decimals, and dynamic values).{{add 'abc' 2 (queryParam 'type')}}
: In this example, any invalid values (non-numeric or NaN) will be ignored and treated as zero.
subtract
The subtract
operator subtracts subsequent numbers from the first number. This is useful when you need to calculate differences between values, such as remaining balance, deductions, or similar operations.
Example:
{
"remaining": {{subtract (queryParam 'balance') (queryParam 'withdrawal')}}
}
If the request URL contains ?balance=1000&withdrawal=200
, the response will be:
{
"remaining": 800
}
Additional Examples:
{{subtract 100 25 10}}
: Subtracts 25 and 10 from 100, resulting in 65.{{subtract (body 'total') 5}}
: Subtracts a static value from a dynamic value retrieved from the request body.{{subtract (queryParam 'initial') (queryParam 'deduction') 50}}
: Subtracts two dynamic values, followed by a static value.{{subtract '200' 50 (body 'discount')}}
: Demonstrates mixed data types, where invalid values like strings ('200') will be treated as numbers if they can be converted, or ignored if not valid.
multiply
The multiply
operator multiplies a list of numbers, which can be a combination of static values or dynamic data extracted from the request.
{
"total": {{multiply (body 'quantity') (body 'unitPrice')}}
}
If the request body contains {"quantity": 10, "unitPrice": 5}
, the response will be:
{
"total": 50
}
Additional Examples:
{{multiply 5 4 3}}
: Multiplies the static values 5, 4, and 3, resulting in 60.{{multiply (body 'quantity') 2}}
: Multiplies a dynamic value from the request body with a static value.{{multiply (queryParam 'length') (queryParam 'width') (queryParam 'height')}}
: Multiplies multiple dynamic values from query parameters to calculate volume, for example.{{multiply '10' 2.5 (body 'factor')}}
: This example uses mixed data types, where non-numeric or invalid values will be assumed as zero.
divide
The divide
operator divides the first number by subsequent numbers. It's useful for calculating ratios, averages, or percentages.
Example:
{
"average": {{divide 100 2}}
}
This divides 100 by 2, resulting in the following response:
{
"average": 50
}
Note: Division by zero will result in an error, as dividing any number by zero is undefined.
Set Operators
oneOf (random pick)
The oneOf
operator selects a random item from a provided list of values. You can supply any number of values, and this operator will pick one randomly. If no values are provided, it returns an empty string.
In the example below, the operator picks a random status from a list of possible application statuses.
Template definition:
{
"status" : "{{oneOf 'draft' 'in-progress' 'approved' 'rejected' 'cancelled'}}"
}
Generated response:
{
"status": "cancelled"
}
Demo: