We have worked on REST API automation using RestAssured tool. It is Java based library for API testing. It works like in headless mode. User sends the HTTP request using URL and receives the response. RestAssured then allows us to validate response along with its status code and values.
There are different types of HTTP methods types are available in RestAssured like: GET, POST, PUT, DELETE.
Here, we will talk about GET method. If we hit an API for the details of city then response would in JSON format with values like cityName, Temparature etc.
“Temprature”:“10 Degreee Celcius”
Sample code to retrieve data is as below:
RestAssured.baseURI = “”;
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.request(Method.GET, “/London”);
String responseBody = response.getBody().asString();
System.out.println("Response Body is => " + responseBody);
Validate the Response Status using below code:
int statusCode = response.getStatusCode();
Now, here you will be interested to validate the values of a parameter from JSON response. Please find below mentioned code to do the same:
JsonPath jsonPathEvaluator = response.jsonPath();
String city = jsonPathEvaluator.get(“City”);
Hope this information is helpful for you.