Embedded Blocks

You can create your own variables with code blocks that can be embedded in the main body of the response. You can also generate arrays of data using loops.

Your first block

Let's create a list of reviews using blocks. First, let's fill Body with the main data structure:

{
  "errors": null,
  "responseData": {$reviews}
}

The reviews variable does not currently exist. Let's create the first block and name it reviews.

The block consists of the following components:

  • Text field for the value. The functionality of the field is identical to the main field for the response body and supports variables. Important note - if the number of iterations is greater than 1, then the text value must be valid JSON.

  • Iterations - the number of JSON objects in the array. As stated earlier, plain text does not support more than 1 iteration.

  • Variable name is a unique name that you will use in the main body of the response. Check that the variable name does not conflict with standard variables.

That's all! The variable has been created. In the response we will see 10 reviews, with random values. If necessary, create a render cache so that you can freeze the response.

Use 1-iteration blocks to keep your main response block clean.

Use the embeddedLoopIndex variable to get the iteration index. This can be useful when constructing arrays of objects with integer IDs

Below is the result of a request to our endpoint. With each request, we will receive different data but the same IDs, because we use a special variable for blocks. This way you can create dynamic responses of any structure.

{
  "errors": null,
  "responseData": [
    {
      "id": "1",
      "email": "[email protected]",
      "createdAt": "474847105",
      "content": "Architecto dolorem doloribus sunt ut itaque. Consectetur natus sit quo voluptatem maiores neque ducimus."
    },
    {
      "id": "2",
      "email": "[email protected]",
      "createdAt": "671988494",
      "content": "Quis eos quia rerum rerum alias molestiae laboriosam. Voluptas debitis rerum dolores asperiores. Inventore similique saepe repudiandae."
    },
    {
      "id": "3",
      "email": "[email protected]",
      "createdAt": "159056457",
      "content": "Velit minima dicta quidem ratione velit. Eos facere quod et eaque sit illo porro. Qui aperiam ea quas tempore suscipit."
    }
    ...
  ]
}

Search and render cache

Response body created using blocks supports search based on URI parameters.

As mentioned earlier, with each request we generate new data if we use variables. If we want to search our body, the right thing to do is to โ€œfreezeโ€ the response. For this you should use Render cache.

Last updated