Create a MultiHTTP check
This tutorial shows how to create your first MultiHTTP check. MultiHTTP checks provide a form-based approach for defining multi-step tests. They’re great for testing workflows, and give you the flexibility to pass data from one request to the next. For example, if you have an e-commerce application, you can create a MultiHTTP check to test a user workflow that would log in to your application, add an item to the shopping cart, check out, and then log out of your application.
In this tutorial you will:
- Create a MultiHTTP check using the
test-api.k6.io
service. The check will have three requests:- Log in using a pre-defined
synthetics_multihttp_example
. - Add a crocodile object to the user’s list, then delete it.
- Log out.
- Log in using a pre-defined
- Set up assertions for each request to ensure they’re working as expected.
Before you begin
- A Grafana instance with Synthetic Monitoring set up.
Note
This tutorial uses thetest-api.k6.io
service, which is a public shared environment. You can use it and follow along this tutorial, but it’s recommended to avoid high-load tests. The service is also open source if you’d like to deploy a private instance.
Create a MultiHTTP check
To create a MultiHTTP check:
- Open your Grafana instance.
- On the left-hand menu, select Testing & Synthetics.
- Click Synthetics.
- Click Add new check.
- Select Create Multi Step check.
After that, you will see the New Multi Step check page where you can configure your new check. The first step is to define the check:
- Fill out Job name.
Log in to an application
The Requests section is where you can configure the specific endpoints you want the MultiHTTP check to test.
For this tutorial, the first step is to make a log in request to the test-api.k6.io
application:
Under Request target, select POST in the methods list.
Under Request target, enter
https://test-api.k6.io/auth/cookie/login/
in the input field.Click Request options
Select the Request Body tab and fill out the following fields:
- Under Content type, enter
application/json
. - Under Request body payload, copy and paste the following payload:
{ "username": "synthetics_multihttp_example", "password": "synthetics_multihttp_example" }
- Under Content type, enter
Note that this login call will create a session cookie, which is needed to authenticate subsequent calls. k6 handles the session cookie just as a browser would. It transparently manages the receipt, storage, and transmission of cookies.
Create an object and extract a variable
The next step is to configure a POST
call to create a crocodile in the test application, and then grab the newly created crocodile ID to use in the next request and delete it.
To create a crocodile in the test application:
Click + Add request below the first request you created in the last step.
Under Request target set the method to
POST
.Under Request target enter
https://test-api.k6.io/my/crocodiles/
in the input field.Click Request options.
Select the Request Body tab and:
- Set Content type to
applicaton/json
. - Set Request body payload to:
{ "name": "synthetics test object", "sex": "F", "date_of_birth": "2024-05-01" }
- Set Content type to
Before adding the next request, you need to create a variable to store the crocodile object ID:
- Click Set variables.
- Click + Add variable.
- Set Variable name to
objectId
. - Set Variable type to JSON path.
- Set Variable expression to
id
.
Delete an object using previously set variables
To delete the crocodile you just created in the test application:
- Click + Add request below the second request you created in the last step.
- Under Request target set the method to
DELETE
. - Under Request target enter
https://test-api.k6.io/my/crocodiles/${objectId}/
in the input field.${objectId}
is the variable that you created in the previous request. When the check runs, that will get replaced with the value for that variable.
Log out of an application
The final step is to log out of the application. To do that:
- Click + Add request below the request you created in the last step.
- Under Request target set the method to
POST
. - Under Request target in the input field enter
https://test-api.k6.io/auth/cookie/logout/
.
Define uptime with assertions
It’s also important to set up Request Assertions for each request you create. That way you can have Synthetic Monitoring automatically validate things such as the response code from your request, or that the response body includes a specific property and value.
To set up these assertions you have to go to the Define uptime section:
- Click the Define uptime option in the sidebar.
Here you can see the three requests you created in the previous section, and you will set up assertions for each one.
For the login request you will add three assertions. Under the First request titled POST https://test-api.k6.io/auth/cookie/login/:
Click + Add assertion below the first request.
For the first assertion, set:
- Assertion type to Text.
- Subject to HTTP status code.
- Condition to Equals.
- Value to
200
.
Click + Add assertion.
For the second assertion, set:
- Assertion type to JSON path value.
- Expression to
first_name
. - Condition to Equals.
- Value to
synthetics_multihttp_example
.
Click + Add assertion.
For the third assertion, set:
- Assertion type to JSON path value.
- Expression to
email
. - Condition to Equals.
- Value to
synthetics_multihttp_example@test.com
.
For the creation request you will add two assertions. Under the Second request titled POST https://test-api.k6.io/my/crocodiles/:
Click + Add assertion.
For the first assertion, set:
- Assertion type to Text.
- Subject to HTTP status code.
- Condition to Equals.
- Value to
201
.
Click + Add assertion.
For the second assertion, set:
- Assertion type to JSON path value.
- Expression to
name
. - Condition to Equals.
- Value to
synthetics test object
.
For the delete request you will add one assertion. Under the third request title DELETE https://test-api.k6.io/my/crocodiles/${objectId}/:
Click + Add assertion.
For this assertion, set:
- Assertion type to Text.
- Subject to HTTP status code.
- Condition to Equals.
- Value to
204
.
For the logout request you will add one assertion. Under the third request title POST https://test-api.k6.io/auth/cookie/logout/:
Click the + Add Assertion tab.
For this assertion, set:
- Assertion type to Text.
- Subject to HTTP status code.
- Condition to Equals.
- Value to
200
.
Now, for every check run, Synthetic Monitoring will validate those assertions.
Set probe locations and frequency
Next, you have to configure the Probes, which represent the locations where you want to run your test from, and how frequent you want your check to run:
- Click Execution in the sidebar.
- Select at least one probe from the Probe locations drop-down.
- You can leave the Frequency fields with the default value.
Test and save your check
You can click Test at the end of the screen to have Synthetic Monitoring run your check, and make sure you get a 200 OK
response that shows your check is working correctly. It will test your requests and assertions and show you the results.
When you are happy your check is working correctly click Submit.
Your check will now run from the probe locations that you selected, and with the default frequency value. Your check will make sure that the endpoints you configured are working correctly, and if you have any issues, you will be able to see them in your check dashboard.
Next steps
Now that you have a MultiHTTP check configured, you can:
- Refer to Create a k6 scripted check to see how you can create a similar check that uses the test-api.k6.io application, but using JavaScript to make your check even more flexible.
- Refer to Check types to get more information about the metrics and options available for the MultiHTTP check type.
- Refer to Analyze results to learn more about how you can visualize and analyze the data collected by your check
- Refer to Synthetic Monitoring alerting to learn how to create and configure alerts in case your check fails.