In our previous article about Locust, we talked about the basics of Locust.io. Right now, you are able to make a GET request from any endpoint by using this amazing tool. Today, I am going to talk about making a POST request and extracting data from HTTP response by using JSON path. After this article, you will learn JSONPath usage with Locust.

When you make a GET request to an endpoint, you only pass the endpoint url to locust’s get method. In order to do a post variable, you need to provide those variable as second parameters to locust’s post request. This variable can be a JSON data, form parameter or a multi-form data or maybe all of them.

GET Request Example

POST Request Example

We are going to use https://jsonplaceholder.typicode.com restful web services to demonstrate our example.

Make POST Request with JSON Body

As the web service description says in https://github.com/typicode/jsonplaceholder#how-to, to be able to make a post request to /posts endpoint we need to send below JSON body.

Then we should use below snippet. As you notice, we added the json key before the variable. So Locust will evaluate this body as a proper JSON file.

Extract the Response

According to the specification, our response body will be:

Let’s extract the title by using the JSON path. To do so, you need to get the HTTP response object and evaluate it.

Here’s how you can do it.

First, you need to assign the response to the output of post request. Secondly, you should convert the response body to a JSON string. Finally, you should extract the desired value by using a JSON expression.

The last line is going to printout the result on the terminal. It’s optional.

JSONPath Usage with Locust

Make POST Request with Form Data

Let’s think that our web services accepts form data parameters, not JSON strings. How you are going to change your implementation? As you might notice, we have a JSON key before the request body. We need to change it to the DATA key. So our implementation changes to that.

Now we are sending title, body and userId parameters as formData parameters. Evaluating the response is the same.

Happy testing with Locust!