Skip to main content

Block Helpers In Template Engine

Repeat

Beeceptor adds an advanced feature known as the Repeat block in the template engine. This allows you to simulate loop constructs while generating mock data. This feature is particularly beneficial when creating a response with JSON arrays, GET API call to retrieve objects or search results in an API response.

Syntax

{{#repeat count}}
content
{{/repeat}}

Here, the content is the text block to be repeated for the given count times. The argument count has to be a number. A comma will be added between consecutive content blocks.

Examples

Generating a JSON array of three users objects.

Template used:

[
{{#repeat 3}}
{
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
},
{{/repeat}}
]

Response generated:

[
{
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
},
{
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
},
{
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
}
]

Technical Details

Here's a detailed technical breakdown of how the Repeat Block functions:

  1. Fixed Iteration Count: By giving a single numerical value as an argument, the repeat block will execute a predetermined number of iterations. This results in consistent output for each request, with the contents of the repeat block duplicated exactly per the specified iteration count.
  2. Random Iteration Range: If you give two numerical values as arguments, it activates a random iteration mode. The system will randomly select a number within this range, leading to a variable number of iterations. Thus, each request generates a unique output as the repeat block's content is replicated a random number of times.
  3. Intelligent Comma Insertion: The repeat block is designed to automatically insert commas (,) between consecutive blocks to ensure the generation of a syntactically correct JSON array or a item list of items. In addtion, it smartly omits the comma after the last block to maintain JSON validity. You can override this behavior by adding comma=false in the repeat argument, which disables the automatic comma insertion. Refer to an example in the table above.
  4. Iteration Cap: The repeat block enforces a maximum limit of 1,000 iterations. If you specify a value exceeding this limit, it is automatically adjusted down to 1,000. This cap ensures efficient performance and avoids excessively large data outputs.
  5. Contextual Keywords: The block supports dynamic keywords such as @index and @total, providing you with contextual information about the current iteration's index and the total iteration count, respectively. This feature is especially useful for labeling or indexing elements within the JSON array output, like assigning unique id fields.

Video Demo

If & Else

The if helper in Beeceptor allows you to conditionally emit content in HTTP responses. When you use this helper, Beeceptor evaluates its argument. If the result is false, undefined, null, an empty string (""), zero (0), or an empty array ([]), the block of content associated with this if helper will not be displayed.

Syntax

{{#if expression}}
content
{{/if}}

Here, the content is the text block will be returned in the HTTP response only when the expression is satisfied. The result of the expression should be non-empty and non-zero to be satisfied. Refer to the examples on how to build expression logic.

You can build complex checks for expression. For instance,

  • to check the presence of a field in a POST request's body, use an expression like (body 'status'). This parses the request body as JSON and evaluates the status field's value.
  • to check the value of a field in the POST request's body, use a sub-expression (eq 'paid' (body 'status')).

Examples

In the following example, the template checks the order status, and returns additional fields in JSON. You can see a conditional field amountToCollect. When the order status is paid, amountToCollect is set to 0, else it is equal to the order amount.

Template used:

{
"customerId": "{{body 'customerId'}}",
"productId": "{{body 'productId'}}",
"quantity": {{body 'quantity' '1'}},
"status": "{{body 'status'}}",
"amount": {{body 'amount'}},
{{#if (eq 'paid' (body 'status'))}}
"amountToCollect": 0,
{{else}}
"amountToCollect": {{body 'amount'}},
{{/if}}
}

Payload for the POST request:

{
"customerId": "1234",
"productId": "PRODUCTID",
"quantity": 3,
"status": "paid",
"amount": 30
}

Response generated:

{
"customerId": "1234",
"productId": "PRODUCTID",
"quantity": 3,
"status": "pending",
"amount": 30,
"amountToCollect": 0,
}

When the status field in the request body has value paid, the template engine emits "amountToCollect" : 0. Else, the value specified in the amount field is replicated for amountToCollect.

Operators

The comparison syntax uses prefix-based notation, where the operator precedes the arguments. Explore these operators and their usage to craft flexible and dynamic mock responses that fit your specific needs.

eq

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

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

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

Technical Details

Here's a detailed technical breakdown of how the Repeat Block functions:

  1. At present the template engine only supports eq operator. The support for more operators will be added soon.
  2. You can pull values from query parameters or request body, in the expression clause.