This article covers:


Web service introduction

Getting started with restassured API

GET method call in restassured

POST method call in restassured

Passing payload in json format





What is a webservice?


A web service is a way of communication among different systems or different applications. It is a piece of code written to exchange information through XML among different independent systems. It is a web component that exchanges information with other independent web components.


One classic example of web service is ticket booking portal- : www.makemytrip.com
When a user who want to buy a flight ticket visits www.makemytrip.com (MMT) and enter his travel details like date of journey, destination, no. of passengers, etc. then what MMT does is it sends service requests in the form of XML or JSON to various airline web services which is nothing but the web component of their websites. These different airlines return a web service response in the form of XML or JSON. This response contains the flight details on that particular date, no. of seats available, price. MMT renders this response data to the UI and the user gets all the info related to available flights on that particular date. In this way, two different websites communicate with each other.


In web service or API testing, we generally check whether the two independent systems are able to communicate or not. In this communication process the web service response headers, response code, response body, content type are extracted from response object and validated. Also, a check has been made to validate whether the response data is rendered properly or not by the AUT (application under test).

There are various types of service request calls, some major ones are:

GET Request:
POST Request:
PUT Request:
DELETE Request:

These requests are made using base URL:
Here
https://www.makemytrip.com/
is an example of base URL:
Anything appended at the end of this url as a parameter makes it an end point url.

https://www.makemytrip.com/flight/search?itinerary=PNQ-IDR-03/10/2020&tripType=O&paxType=A-1_C-0_I-0&intl=false&cabinClass=E

here we are sending the request parameters as itinerary = pune to indore, date, class etc. GET is a simple request where we're simply requesting some info from other independent systems. Here in this e.g. MMT is requesting other airlines to give the details of flights available on the requested date. Airlines on the other hand respond with response data which contains a response code, response header, response content, etc. This response content is parsed and rendered to UI by MMT. A successful GET call returns a response code of '200 OK'.
POST call inserts something in DB residing at some server. You send a POST call through the endpoint URL along with JSON data. A successful POST call returns a response code of '201 OK'. Use PUT call, if you want to update existing data in DB.



Getting started with io-restassured:


io.rest-assured is a set of libraries for API testing. If you are using maven, include the following dependencies in your pom.xml file. If not then you can download the jar files and add rest-assured libraries in the build path.

json-simple for validating JSON response and rest-assured.

restassured dependencies



How to make a GET call in restassured?


Below is the code snippet of a simple GET request using io.rest-assured libraries. Here there are 3 main components. First is the base URL through which you can request the weather of a city. The second is the request object and the third is a response object. The request object is used to send requests to the server. After declaring a base URI, RestAssured.given() a RequestSpecification object. You can make a request through this RequestSpecification object and store the response in the Response object. Now lastly you can parse this response object to extract various info like response code, response body, headers, and so on.

GET request using io.rest-assured



How to make a POST call in restassured?


Below is the code snippet of a POST request using io.rest-assured libraries. In POST request you store some data in DB. To do this we use JSON object to specify the data which we want to store in DB and then convert that data in JSON form. And lastly, send that data to the server using the RequestSpecification object.

POST request using io.rest-assured





How to pass JSON payload in API request


Alternatively, we can pass the entire payload in the form of JSON in the request body. The payload is nothing but the data in JSON format that we want to store in the database. Using rest-assured APIs, we can pass the entire payload in the request body and post it over the DB server. Below is the code snippet to pass the payload in the request body.

1. Get a base URI

base URI

2. Create a request object

Request Object

3. Create a JSON object

JSON Object

4. Create a file object and pass the JSON file in the file argument. This JSON file contains the payload (i.e. the data which we want to post over the DB server).

file Object

5. Create a request header with content-type as application/json

Request Header

6. Now pass the file object in the request body

passingPayload

7. Lastly, make a request call through the request object and specify the POST as method type along with the endpoint of the base URI.

define Method Type



In the next article we'll see authorization and authentication through rest-assured libraries.