GET and POST requests Automation using Rest-Assured

Reading Time: 3 minutes

Why POST request is different from GET request:-

When we are using POST requests in services then it is very common that we are sending some business data to the server. We don’t expose this information via query parameter for better. POST method is secure because data is not visible in URL String and can be safely encrypted using HTTPS for further security. We will see Request and Response Automation in this blog to cover API testing aspects.

When it comes to the real-world application then POST request can have a very complex body structure and can be very big in size too. Handling complex data and it is the most used and important HTTP method. As it is providing one layer of extra security, is the reason for passing sensitive business-related data. POST method is used for creating a new record in the application database.

Types of parameters in REST service:-

  1. Path Parameter
  2. Query Parameter
  3. Header Parameter
  4. Request Body Parameter

Path Parameter :-

Parameters within the path of the endpoint, before the query string (?). These are usually set off within curly braces. Following is the example for the query parameter.”https://myserver.com/v1/service/myresource/user/{user}/bicycles/{bicycleId}”

Query Parameter :-

Query parameter appears after “?” in the URL. It comes after resource path. Following is the example for query parameter. “https://myserver.com/resource-name?param1=value1&param2=value2”

Header parameter :-

Parameters included in the request header, usually related to authorization. Example for Request body parameter is :- Appkey = {appkey}

Request Body Parameter :-

Parameters included in the request body. Usually submitted as JSON. Example for Request body parameter is :-

{
"days": 2,
"units": "imperial",
"time": 1433524597
}

In this blog, we are going to discuss more about POST request testing and automation using RestAssured. Request Body parameter is also called payload. So in this blog, we will refer to request body parameter as Payload of the post request. we will see how we can send payload from client to server. There are two main ways by which we use payload in POST request.

  • Payload as String
  • Payload as Object.

Paylaod As string:-

This is one of the basic approach in which we use simple JSON payload as a string. We pass direct JSON string in the POST method and pass the data in that JSON string.

Request and Response AutomationPayloadAsString

Payload as Object :-

Payload as string is the second way by which we can send payload to the server. In this method, we pass an object to the POST Method. We need to create a POJO class for JSON and then we can set data as per the requirement.

payloadASObject

In my opinion, we should use the object as the payload as it provides better management for data data-driven testing and also provides better management. Rest-assured provides auto serialization and de-serialization but we can use any other serialization for object mapping.

Response Assertion :-

So, we know how to make any REST requests. Now we will see how to play with the response of the REST service. When we are performing functional testing then we need to validate the data which we are getting in Response. Let’s see one example of this.

basic-assertion

Soft Assertion :-

Soft assertion is a TestNG feature. it helps to collect all the assertions throughout the @Test method. We have to use assertAll() method to see the assertion result at the end of the test. When we are using SoftAssertion then exception will not occur just after any assertion fails. Test execution will continue with the next step after the assert statement. Let’s see this in the following example.

Soft-assertion

Conclusion:-

Request and Response Automation is one of the major areas. using RestAssured library solves the purpose of the Automation solution. RestAssured provides one of the seamless capability to perform all functional testing aspects. We can achieve End To End functional testing to Smoke/Sanity testing using this tool.

Refrences :-

https://stackoverflow.com/questions/50395694/testng-soft-assert,

https://github.com/rest-assured/rest-assured/wiki/usage

Written by 

QA Consultant