top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

GET, POST and DELETE Method in Rest Assured - API Testing


What is Rest Assured?


REST Assured is a Java library for testing RESTful APIs. It is widely used to test JSON and XML based web applications. Furthermore, it fully supports all methods including the GET, PUT, POST, PATCH, and DELETE. REST Assured provides users with Domain Specific Language (DSL) to create powerful tests for REST APIs in an effective manner.

Rest Assured supports only REST Services and cannot support SOAP services.


Let me show you an example by creating a project for API testing using Rest Assured.



Prerequisites settings before creating Rest assured API testing project:

· Install Java in your system

· Install an IDE like Eclipse

· Maven must be installed

· Install TestNG plugins


The following steps will guide how to Create a project for API Testing

Step 1 : Open Eclipse

Step 2: Create a maven Project

Step3: Add dependencies in pom.xml

Step 4: Save Project

Step 5: Add Testing Plugin

Step 6: Create Tests

Step 7: Run and verify


Add the below dependencies to the POM.xml:

<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>json-path</artifactId>

<version>4.2.0</version>

<scope>test</scope>

</dependency>


<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>xml-path</artifactId>

<version>4.2.0</version>

<scope>test</scope>

</dependency>


<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>json-schema-validator</artifactId>

<version>4.2.0</version>

<scope>test</scope>

</dependency>



Selecting The Appropriate Method


Here are the three HTTP methods that correspond to these actions.

GET Method : Read

Post Method : Create

Delete Method: Delete


Let’s see an example of GET , Post and Delete Method using Rest Assured


· Create a Maven Project

· Add Dependencies in pom.xml

· Then create a TestNG class



Request Type: GET

URI: https://dummy.restapiexample.com/api/v1/employees


After creating a TestNG class let’s write the Rest Assured API Automation code


Step: 1 Let’s specify the base URI using the predefined class ResAssured.baseURI


Step: 2 Inorder to send the URI as GET request, we need to create a request object. We have predefined class in Rest assured as RequestSpecification


Step: 3 Using RestAssured.given() and httpRequest method we are sending the request to the server


Step: 4 Response object will send the request using the GET Method




The below code is used for GET method to validate the response.

Code Snippet


@Test
  public void f() 
  {
   //Specify base URI            
   RestAssured.baseURI="https://dummy.restapiexample.com/api/v1/employees;
                  
 //Request object
    RequestSpecification httpRequest=RestAssured.given();
                  
   //Response object
 Response response=httpRequest.request(Method.GET);
                  
  //print response in console window
                  
  String responseBody=response.getBody().asString();
   System.out.println("Response Body is:" +responseBody);
                  
//status code validation
int statusCode=response.getStatusCode();

 System.out.println("Status code is: "+statusCode);
   Assert.assertEquals(statusCode, 200);
                  
//status line verification
  String statusLine=response.getStatusLine();
  System.out.println("Status line is:"+statusLine);
  Assert.assertEquals(statusLine, "HTTP/1.1 200 OK");

  }
}


The Response Body is shown in the below picture















STATUS CODE : 200

Status Line: HTTP/1.1 200 OK"


If the status code is 200 then the server has successfully processed the request.




Request Type: POST

URI: https://reqres.in/api/users?page=2


The below code is used for POST method to create a new record


After creating a TestNG class let’s write the Rest Assured API Automation code


Step: 1 Let’s specify the base URI using the predefined class ResAssured.baseURI


Step: 2 In Post method we need to send the request first inorder to get the response hence using Request object in the code


Step: 3 In Post method we need to send parameters like id, email id, first name and last name etc…to create a new entry or record


Step : 4 Body is in JSON format hence we are using JSONObject


Step: 5 Request payload sending along with post request we need to use requestParams.put


Step: 6 Inorder to specify the Content type we need to add the header

httpRequest.header("Content-Type","application/json")


Step: 7 Response object will send the request using POST Method




Creating the new record by sending the below parameters


Body:
{
“Id” : 13
“email”:”michael.lawson@reqres.in”
“first name”:”Michael”
“last name”:”Lawson”
“Avatar”:”https://reqres.in/img/faces/7-image.jpg"
}

Code:


@Test
  public void f() 
  {
   //Specify base URI                  RestAssured.baseURI="https://reqres.in/api/userspage=2";
                  
//Request object(whenever you want to send request to the server)
  RequestSpecification httpRequest=RestAssured.given();
                  
 //Request payload sending along with post request
  JSONObject requestParams=new JSONObject();
                  
 requestParams.put("id",14);
 requestParams.put("email","nicole.lawson@reqres.in");
 requestParams.put("first_name","Nicole");
 requestParams.put("last_name","Lawson");        
 requestParams.put("avatar","https://reqres.in/img/faces/7-image.jpg");
                
                  
httpRequest.header("Content-Type","application/json");
                  
httpRequest.body(requestParams.toJSONString());                       
 // attach above data to the request
                   
 //Response object
 Response response=httpRequest.request(Method.POST);
                  
 //print response in console window
                  
  String responseBody=response.getBody().asString();
 System.out.println("Response Body is:"+responseBody);
                  
 //status code validation
 int statusCode=response.getStatusCode();
  System.out.println("Status code is: "+statusCode);
 Assert.assertEquals(statusCode, 200);    
                 
                 }
                  
                
  }


The Response Body is shown in the picture below



















STATUS CODE : 201


If the status code is 201 then it indicates Created success status response code and the request has succeeded and has led to the creation of a resource.



Request Type: DELETE



URI: https://reqres.in/api/users?page=2



The below code is used for DELETE method to delete a particular record



After creating a TestNG class let’s write the Rest Assured API Automation code



Step: 1 Let’s specify the base URI using the predefined class ResAssured.baseURI


Step: 2 In Delete method we need to send the request first inorder to get the response hence using Request object in the code


Step: 3 In Delete method we need to send parameters like id# to get the specific record to be deleted


Step : 4 Body is in JSON format hence we are using JSONObject


Step: 5 Request payload sending along with Delete request we need to use requestParams.put


Step: 6 Response object will send the request using Delete Method


Code Snippet:


@Test
  public void f() 
  {
   //Specify base URI
   RestAssured.baseURI="https://reqres.in/api/users?page=2";
              
   //Request object
   RequestSpecification httpRequest=RestAssured.given();
              
  
   //print response in console window//Request payload sending along with post request
 JSONObject requestParams=new JSONObject();
              
  requestParams.put("id",14);
                          
   httpRequest.header("Content-Type","application/json");
              
  httpRequest.body(requestParams.toJSONString()); // attach above data to the request
               
   //Response object
   Response response=httpRequest.request(Method.DELETE);
              
   String responseBody=response.getBody().asString();
   System.out.println("Response Body is:" +responseBody);
              
  //status code validation
   int statusCode=response.getStatusCode();
   System.out.println("Status code is: "+statusCode);
   Assert.assertEquals(statusCode, 204);         
  }
}

The Response Body is shown in the picture below

















STATUS CODE : 204


If the Status code is 204 then it indicates that the server has successfully fulfilled the request.



There are various methods in Rest Assured to do API testing. In this blog you have seen one of the method to perform GET, POST and DELETE for API testing using Rest Assured.



Hope this blog will be helpful to peform API testing using Rest Assured




1,837 views1 comment

1 Comment

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Lakshmi S
Lakshmi S
Mar 07, 2022

very useful blog thanks for sharing :)

Like
bottom of page