Send POST request using REST-Assured

This entry is part 8 of 14 in the series REST Assured Tutorial

In previous tutorial we used postman-echo API, in this tutorial we will use JSONPlace Holder, to send post request using REST-Assured. JSONPlace Holder is an open API, anyone can use it to test their API-Automation code. I will encourage you to visit JSONPlace Holder and understand all the operations allowed, you will get an idea about how an API documentation should be done. Developers will provide you such type of documentation.

POST method is used to create a new resource. We can send information with the request body, which will be associated with the resource. For example a tweet on Twitter, if we are tweeting on twitter, we need to send the text within the request body, there will be some information in the header too.

We will use to post a resource to endpoint.
Below is the data we are sending to POST request in a JSON format. Please do not copy the below data from the webpage directly, write it manually, because if you copy paste directly, you will see some syntax errors in postman.

“userId”: 2,
“id”: 19,
“title”: “this is”,
“body”: “this is REST-Assured Tutorial”

We will also validate the response after sending the POST. After the POST gets successfully processed, server will send us status code 201(resource created), so we will validate it. We will also validate the title  in the response.


Please note while sending the POST request, you will need to mention the content type of your data, i.e. application/json.

Please pay close attention on how we are writing the data in java code. We are using map in java to store all the contents using put() method and then simply sending the map object in body() function.
Here I introduced @BeforeTest annotation, it is a TestNG annotation. The @BeforeTest is used to perform setup task. The tasks which needs to be performed before the testing. The @BeforeTest annotation will get automatically called before the @Test annotation.

Nowe consider a scenario where we need to send 100’s of request to the API, in such case we will need to write get() method with the API URL every time we send the request, like below :

But if you write below code in @BeforeTest, you will need to write only once.

If you write above code,you dont need to store the URL in any variable, REST-Assured will automatically provide the URL, whenever you call any HTTP method, in this case we called post() method, without providing any URL parameter in it.

Series Navigation<< Send GET request using REST-AssuredSend PUT request using REST-Assured >>