POST Request and JSONPath Usage with Locust

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

self.client.get("/posts")

POST Request Example

self.client.post("/posts",VARIABLES_)

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.

{
        "title": "Silence of the Lambs",
        "body": "Thriller Book",
        "userId": 1
}

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.

class Test_1(TaskSet):
    @task(1)
    def users(self):
        self.client.post("/posts", json=
        {
        "title": "Silence of the Lambs",
        "body": "Thriller Book",
        "userId": 1
        }
        )

Extract the Response

According to the specification, our response body will be:

{
  id: 101,
  title: 'foo',
  body: 'bar',
  userId: 1
}

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.

class Test_1(TaskSet):

    @task(1)
    def users(self):
        response = self.client.post("/posts", json=
        {
        "title": "Silence of the Lambs",
        "body": "Thriller Book",
        "userId": 1
        }
        )
        json_var = response.json()
        request_id = json_var['title']

        print 'Post title is ' + request_id

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.

class Test_1(TaskSet):

    @task(1)
    def users(self):
        response = self.client.post("/posts", data=
        {
        "title": "Silence of the Lambs",
        "body": "Thriller Book",
        "userId": 1
        }
        )

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

Happy testing with Locust!

1 thought on “POST Request and JSONPath Usage with Locust”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.