In this tutorial, learn how to write a test that does the following:
- Sends a POST request to the new endpoint
- Creates a check for the response status
Script the Request
The first thing to do is to add logic for the endpoint. To do that, you need to make an HTTP request:
- Import the HTTP module.
- Create a payload to authenticate the user.
- Use the http.post method to send your request with the payload to an endpoint.
To test, copy this file and save it as api-test.js.
Run the script using the k6 run command:
After the test finishes, k6 reports the default result summary.
As an optional step, you can log the response body to the console to make sure you're getting the right response.
Add response checks
Once you're sure the request is well-formed, add a check that validates whether the system responds with the expected status code.
Update your script so it has the following check function.
checks response is 200Run the script again.
Inspect the result output for your check. It should look something like this.
noteUnder larger loads, this check will fail in some iterations. Failed checks do not stop tests.
Rather, k6 tracks the success rate and presents it in your end of test summary.
Next steps
In this tutorial, you've used k6 to make a POST request and check that it responds with a 200 status.
However, these tests make only one request, which doesn't say much about how the system will respond under load. For that, you need to test under load.