Sometimes, when you configure HTTP(S) monitoring or a load test to check your website or API performance, you need to send HTTP requests with a dynamically changing payload. For example, you may need to send a JSON with a unique product ID to add a new product to the e-commerce application’s database, or an actual date and time to a web server, etc. Or, let’s say you need to parametrize a payload to load test your API.

Dotcom-Monitor supports the Razor syntax and data masks to allow users to configure dynamically changing and parametrized HTTP requests for web monitoring and load testing purposes.

Generally, dynamic payload configuring includes two basic steps:

  • Specifying body data with dynamic parameters (JSON, XML, Text, JavaScript, HTML) in the Post Data (Patch, Put) field.
  • Declaring a parsing method in the Prepare Script field. If the parsing method is not specified, the system will process body data as text.

If it is necessary to parametrize the body data of an HTTP request, the corresponding context parameters can be declared in the Prepare Script field or with the Manage Context Parameters option.

Using Razor Engine

JSON data or script content that is passed in the HTTP request’s body can be dynamically changed using the Razor engine syntax. When working with the Razor syntax in the Post Data field, use the @ prefix to mark the following text as a C# code element ( inline expressions, single statement blocks, or multi-statement blocks). To find more information on the Razor syntax, please visit  https://www.w3schools.com/asp/razor_syntax.asp.

In case of any mistakes detected in the Razor syntax while parsing the request body, an error will be generated and added to the test execution report.

Dotcom-Monitor supports the Razor model (defines the structure of a Razor input message) with the following properties:

  • @Model.DeviceID – a variable that returns a unique identifier of the current monitoring device.
  • @Model.TaskID – a variable that returns a unique identifier of the current monitoring task.
  • @Model.SessionID – a variable that returns a unique identifier of the current monitoring session.

You can use context parameters in Razor expressions. To refer to a context parameter in a Razor expression, wrap the reference to the context parameter in the square brackets [ ] and enclose its name in double quotes ” “:

@Model["ContextParameterName"]

To notify the system that data in a request body require to be parsed by the Razor engine, in the Prepare Script field, add the ProcessPostDataByRazor(currentTask) method. Otherwise, the code elements from Post Data will be parsed as text.

Example

Let’s say, we want to send a device ID and a test marker in an HTTP request body, then we need to configure the Post Data field as follows:

{ "Device": "@Model.DeviceID", "ContextTest": "@Model["Test"]" }

Then we need to set the context parameter value and parsing method in the Prepare Script field:

context.Test = "Website Availability Monitoring";
ProcessPostDataByRazor(currentTask);

Where the currentTask parameter does not depend on a task name and has the type of task that is currently processed.

Using Data Masks in HTTP Requests

You can reference context parameters in a POST body by wrapping your parameter name reference in marker signs on either side. So a reference to a context parameter “GUID” will be entered like this:

{"ContextGuid": "%%%Guid%%%"}

Here the percent signs tell the system that this is a context parameter reference and not a part of a string.

To notify the system that data in a request body needs to be parsed by a specified mask, in the Prepare Script field, add the ProcessPostDataByMask(currentTask, “marker signs”). Otherwise, the context parameter will be ignored and passed as text. For the example provided above, the Prepare Script field would contain the following string:

ProcessPostDataByMask(currentTask, "%%%");

Where the currentTask parameter does not depend on a task name and has the type of task that is currently processed.