top of page

Rest Assured-Generation of Bearer Token using data driven approach and Jackson API

shrutihundekar

A payload refers to a data or information that is sent by client in request to server or the data that is returned by the server in response to the request.The payload typically contains the data that needs to be processed or manipulated by the server, such as a JSON or XML object, or sometimes binary data like images or videos. 

The operations we can perform on the APIs are known as CRUD operations which are GET,POST,PUT and DELETE.


C ---> Create ---> POST-->When creating new resource, the payload usually carries data for new entity.

R ---> Read ---> GET--> Fetch data, usually these request do not have a payload(request body)

U ---> Update --->PUT--> Used to update a resource, the payload here has complete updated data

D ---> Delete ---> DELETE -->Delete operations usually do not have payload


Apart from these basic operations on API(Web Service) we have one more operation that can be performed on the APIs which is PATCH, this method is used for partial updates, that is when a specific field has to be modified.


APIs conforming to the REST architectural style is called a REST API (or RESTful API).REST is a set of architectural constraints, not a protocol or a standard. API developers can implement REST in a variety of ways. For testing the REST APIs we have REST-Assured a Java library for testing RESTful web services. It is used to invoke REST web services and validate requests & responses. REST-Assured is an open-source API test automation tool for testing the rest services.


In this article let's see in detail on few important topics:

  • How a payload for a post request can be formed using POJO class

  •  How a JSON string can be formed using Jackson APIs Object mapper class, thus achieving serialization

  • Data driven approach for post request

  • Bearer token generation


First to start with will understand how to create a POJO class and send a payload for post request will consider a following request body :


{

  "password": "string",

  "LoginEmailId": "string"

}


For this request body we have to create a java class, declare the variables password and loginEmailId as private, then we have to generate the getters and setters method. The significance of getters and setters methods are they are used for accessing and modifying the values of private variables. Getter method return the value of respective variable where as setter method sets new value to the variable. Below screenshot has UserLoginPOJO class which has the variables and getter and setter method respectively:



Once the POJO class is created, we have to create a java class to send data for the post request, we are calling this class as UserLoginPayload and below is the screenshot:




In this payload class we are reading data from Excel sheet, hence achieving data driven for post request. In the above code first we are reading data from the excel using row.get and then using the object of POJO class we are setting the values read from the excel. Now the values for password and userLoginEmailId are read from excel and are in the form of java objects, for the post request we have to send the request body in json format. Conversion of the java objects to json is done by ObjectMapper class.Let's see what is Object Mapper class in detail and the dependency used.


JSON is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. There is a java library called Jackson API, where Object Mapper is the class in this Jackson API. Jackson API is a high performance JSON processor for Java. We can perform serialization, deserialization , reading a JSON file, writing a JSON file and a lot more things using Jackson API.

To use Jackson API, we need to add below dependency to our pom.xml:


<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.13.4</version>

</dependency>


The Object Mapper class in the Jackson Java Library provides methods to convert Java objects to JSON and vice versa. To convert java object to JSON ,Object Mapper class has a method called "writeValueAsString()" .This method serializes Java object into JSON string. Thus achieving the serialization of data.

We can Pretty print the JSON generated using method "writerWithDefaultPrettyPrinter()" present in Object Mapper class.

 

 The code for reading the values from excel is in the below screenshot:


The maven dependency required to read data from the excel is:


<dependency>

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

    <artifactId>poi</artifactId>

    <version>5.2.5</version>

</dependency>

<dependency>

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

    <artifactId>poi-ooxml</artifactId>

    <version>5.2.5</version>

</dependency>


The post request made, consists of login details to an API. APIs can be accessed with different authentication methods like Basic Auth, OAuth, Bearer Token Authentication. Here in this API call we have a bearer token authorization.Bearer token authentication is an authentication method where a token is generated and used to authorize requests. Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string and every time before accessing the API, this post request with login details has to be made which generates the token and this token is captured and used for further interactions with the API.


In rest assured we have a Response interface through which we are capturing the token generated using the below code:


Response response = RestAssured.given().baseUri(URLs.BaseURL)

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

.body(Jsonbody)

.when()

.post(URLs.loginEndpoint);

System.out.println(response.asString());

String token=response.jsonPath().get("token").toString();

return token;


Note: URL here is another java class which consists of base url, endpoints, path of excel reader file.


The maven dependency required for rest assured is:


<dependency>

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

    <artifactId>json-path</artifactId>

    <version>5.4.0</version>

    <scope>test</scope>

</dependency>


An API is an agreement between you and the software that you'll exchange information using set standards, where standard what we observed in this article are:

  • Sending a post request payload, password and loginEmailId in the form of JSON

  • Capturing the bearer token generated for our further interactions with the API using Rest Assured


With these initial set up to an API which is similar to logging into gmail account(which again is an API) by entering email id and password to proceed using the gmail services.With this initial setup to an API we can interact and test the API using Rest Assured.


Keep Exploring and Happy Coding with Rest Assured!







+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page