How To Learn About API testing with REST Assured | Devstringx

Back to Blog
API testing with REST Assured

How To Learn About API testing with REST Assured | Devstringx

API’s (Application programming interface) plays important role in development. An application programming interface is an interface that defines interactions between multiple software.

API’s are an essential part of any software that is why it becomes necessary to test APIs manually or Through Automation.

Rest Assured gives you the functionality to test REST API with java libraries and Maven Integration. Rest Assured has multiple methods to fetch data from every part of the request/response body.

Configuration Needed for Rest Assured

Step1- Install Any IDE (Eclipse, IntelliJ J) & Install Java.

Step 2- Install Maven.

Step3- Create Maven Project on Eclipse & on Pom.xml, Add the below-mentioned dependency.

If you have Java version<9 then Add,


Now you are good to go with Rest Assured.

Rest Assured Syntax-

Rest Assured is a very simple, Understandable, and like BDD (Behavioural Driven DevelopmentFramework.

param("param1", "param2").
header("h1", "h2").
body("param1”,” param2", equalTo("z"));

Given()- On this keyword, you can pass query params, headers, cookies, etc if they are needed.

When()-On this keyword, the ‘when’ keyword marks the premise of your scenario. For e.g.- ‘when’ you get/post/put/delete something.

Method()-On this Keyword, Add any method which you want to use like (Get, Post, Put, Delete)

Then()- Add assertions and validations here.

Use of Data Provider in Rest Assured

TestNG Framework has some very useful annotations. We can use @dataProvider annotation with Rest Assured.

To use @DataProvider annotation with Rest Assured, you just have to parameterize @Test annotation and provide the name of the Data Provider you created. Your test method should have the exact number of parameters matching the number of columns you entered while declaring a Data Provider.

For e.g.-

Using data provider with Rest Assured –

@Test(dataProvider = "employee-data")
public void addEmployees(String emp_name, String emp_id) {
" {\r\n" +
"   \"employee_name\": \""+emp_name+"\",\r\n" +
"   \"emp_id\": "+ emp_id+"\r\n" +
" }"
Declare Data Provider -
@DataProvider(name=" employee-data")
Object[][] employeesData(){
return new Object[][] {
{"Ram", "1"},
{"Shyam", "7"},
{"Mahesh", "10"}

Read Also:- Robot Framework and Features | Automation Framework

How to Verify Response code –

On Rest Assured, we have the assertThat() function to verify the Status code, response body, etc. By using assertThat(), We can assert the response code of any API.


For verifying status code is 200, we use the keywords — assertThat().statusCode(expCode);

public static void getResponseStatus(){
int statusCode= given().

How to Verify Response Data-

On Rest Assured, we have Response Interface which contains two methods to get the Response Body

  • Response.body() : returns ResponseBody

Using this class, we can get and validate parts of the Response Body.

Below is the Simple Example to check whether the response Body contains a specific string –

RestAssured.baseURI = “ “;
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get(“/data”);
//it will retrieved the body of the Response
ResponseBody body = response.getBody();
String bodyAsString = body.asString();
// it will whether response body contains the Test string or not.

Get Request using Rest Assured-

GET Method is used to fetch data from the Server.


//specify the baseURI
RestAssured.baseURI = “ “;
//Get the RequestSpecification of the request that you want to sent to the server.
RequestSpecification httpRequest = RestAssured.given();
//Here Method type is defined as GET
Response response = httpRequest.request(Method.GET, “/Test”);
//it will fetch the response body from the server.
String responseBody = response.getBody().asString();
//now let us print response body to check response which we have received from server.
System.out.println(“Response Body is “ + responseBody);

Post Request using Rest Assured –

POST Method is used to send data to the Server POST request usually result in changes on the Server like the creation of new data or updates to existing data. In the POST request, The data is sent to the server in the body of the HTTP request. The type can be XML, JSON which is defined by the Content-Type header.

Example –

RestAssured.baseURI ="";
RequestSpecification request = RestAssured.given();
// It will Create a JSON request which contains all the fields
JSONObject requestParams = new JSONObject();
requestParams.put("FirstName", "Test");
requestParams.put("LastName", "User");
// Send the post  request and check the response
Response response ="/register");
int actualStatusCode= response.getStatusCode();
Assert.assertEquals(actualStatusCode, "201", ”Status Code is same as expected.”);

Read Also:- Mobile Test Automation Using Appium Python

PUT Request using Rest Assured –

The PUT request method creates a new resource or substitutes a representation of the target resource with the request payload.

Example –

Int age=24;
RestAssured.baseURI ="";
RequestSpecification request = RestAssured.given();
JSONObject requestParams = new JSONObject()
;requestParams.put("name", "Zion");
requestParams.put("age", 23);
Response response = request.put("/update/"+ age); 
Assert.assertEquals(response.getStatusCode(), 200);

DELETE Request using Rest Assured –

The DELETE method requests to delete the resource from the server which is recognized by resource URI.

Example –

int age = 24;
RestAssured.baseURI = "";
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json");
// send the delete request and check the response
Response response = request.delete("/delete/"+ age);
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, 200);
Assert.assertEquals(jsonString.contains("Record is deleted successfully."), true);

Share this post

Back to Blog