Rest Assured API POST Method

Mani B
5 min readMar 12, 2021

--

RestAssured POST method with Data Driven Approach passing data through Excel sheet — PART-3

This is my third blog in the series of Rest Assured Method. Links to earlier logs:

1. REST ASSURED API testing GET METHOD PART-1

2. GET METHOD with Key and Values pair combination PART-2

To test and validate the API while using POST request with Rest Assured and using Data-Driven approach. I have used the Data-driven framework so while executing POST requests, all the Test data which was stored in the Excel sheet was passed to the POST request.

**Request body is actually the payload which can be sent to the API as part of POST request, In this blog Request body i.e. Payload passed as String.

For this purpose, I have used this API.

https://reqres.in/api/users.

POST method is used to create a record in the server‘s data.

Examples are when submitting forms or filling in the details of Name, address, phone, or more details in the form on a webpage.

The response code when the data is submitted through POST request is “201,” so the server is added newly submitted values to the already existing data on the server.

Advantages: This Rest Assured POST method is very useful while sending the request body (Payload as String)using the Data-Driven Approach to send DATA to the POST Request and get response “201” as creating data on the server-side.

Special Features:

· POST request is used to create a new record in the API data.

· In the Post method request body payload is passed as a “STRING” to the API as part of POST request.

· POST method is very useful when using the Data-Driven Framework with Selenium (Passing the Test data through Excel sheet) while creating a POST request.

· This method actually sends the payload to the API and sends it as String data like row-wise from the Test Data of Excel sheet.

Tools required: JAVA, Eclipse IDE or Intelli J with Maven, and TestNG Plugin, MS Excel sheet

For DataDriven Framework used Maven dependencies like with Apache POI and Apache OOXML in my Pom.xml structure and updated the maven project.

Steps to execute:

1. Create a Maven Project in the Eclipse IDE.and added TestNg plug-in must be installed. You can install from Marketplace or add a dependency and also created a separate package to have more precise structure in the Project for POST Request.

2. In this blog highlight is to send Post Request as Payload String and send Full Test Data with excel sheet so created a separate package for POST Request.

3. In the POM.xml add dependencies for Rest Assured and also added Dependencies for Apache poi and Added Apache ooxml. Added these dependencies in the pom.xml

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>4.1.2</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>4.1.2</version>

</dependency>

<dependency>

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

<artifactId>rest-assured</artifactId>

<version>4.3.2</version>

<scope>test</scope>

</dependency>

The first two dependencies are for exporting the Test data from an Excel sheet. The last dependencies are for RestAssured.

4. In the project once Maven is updated have all these imports.

import io.restassured.RestAssured;

import io.restassured.response.Response;

import io.restassured.specification.RequestSpecification;

import static io.restassured.RestAssured.given;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.testng.annotations.Test;

5. Used TestNg Selenium Framework with @Test annotation to process the method.

6. Test data in Excel sheet as:

With Name and Job Title having 2 columns and 8 rows of data. Get the File path of the Excel sheet i.e. Testdata stored in Variable

String filepath=”C:\\excelsheet\\datareqprofessional1.xlsx”;

If more fields needed, can be added to the excel sheet.

7. Basically by using Apache poi s import created three static variable

static XSSFSheet sheet;

static XSSFWorkbook wb;

static XSSFCell cell;

8. Created a file object and send the file path as Parameter.

File fileobj=new File(filepath);

9. The method to read Excel files. Here in the above code used the list to get data from the Excel sheet and added it to Namelist and Joblist.

11 . Also used two Arraylist to store Test data from Excel into the ArrayList.

ArrayList<String>Namelist=new ArrayList<String>();

ArrayList<String>Joblist=new ArrayList<String>();

12. Also created a class called Employee and Used this Namelist and Joblist in the constructor to initiate.

13. The last segment of code created and initiated a counter and status code.

14. Employee class:

Employee emp=new Employee(Namelist ,Joblist);

Response response=given().body(emp).contentType(“application/json”).post(“https://reqres.in/api/users");

Passed the object emp as the request body.

15. Used this piece of code to check how many times the request was submitted from the excel sheet reader as a row?

The console printed no of times it executed as 8.

(Equal to the no of rows in Excel sheet Testdata).

16. The complete code:

POST method with Data Driven Approach passing data through Excel sheet

Console Output is:

Console output: REST ASSURED POST METHOD DATA-DRIVEN TESTDATA THROUGH EXCEL SHEET

Conclusion:

In the Rest Assured Post Request Method, request from Data driven Framework where Test data is stored in Excel sheet can be sent as Payload a String.

This creates a resource with code 201.

This is a working code for Post Request to execute and get Status code for POST request as 201. Would welcome your feedback and what would you like to hear more.

See you in other methods of API Testing!

Reference: Rest Assured.io(https://rest-assured.io/)

https://mvnrepository.com/

Mani Bhute

--

--

Mani B
Mani B

Written by Mani B

Mani has done Masters in Electronics and currently working in IT Automation SDET .Her area of specialization in Java ,Selenium ,Katalon, REST API ,RestAssured.

No responses yet