In the last two tutorials we have learnt about Response Status Code, Status line and Headers. We will continue with the same example in those tutorials and verify the body if the Response. If you have not gone through the first two tutorials then I would suggest you go through these links

In this tutorial we will learn about How to Read JSON Response Body using Rest Assured? and How to Validate Content of a Response Body?

Read JSON Response Body using Rest Assured

Let us continue with the example of Weather web service that we used in the previous tutorials. When we request for the Weather details of a particular city, Server responds by sending the Weather details of the city as the Response Body. Response interface contains two methods to get the Response Body

Response.body() : returns ResponseBody

Response.getBody() : returns ResponseBody

Using these methods we can get an Object of type io.restassured.response.ResponseBody. This class represents the Body of a received Response. Using this class you can get and validate complete or parts of the Response Body. In the below code we will simply read the complete Response Body by using Response.getBody() and will print it out on the console window.

// By using the ResponseBody.asString() method, we can convert the body

// into the string representation.

System.out.println("Response Body is: "+body.asString());

}

ResponseBodyinterface also has a method called .asString(), as used in the above code, which converts a ResponseBodyinto its String representation. If you run this test the output will look something like this:

Note:Response.body() method does exactly the same thing. So you can even use .body() method in the above code.

How to Validate Response Body contains some String?

ResponseBodycan return the response body in a String format. We can use simple String methods to verify certain basic level of values in the Response. For e.g. we can use the String.contains() method to see if the Response contains a “Hyderabad” in it. The below code shows how to check for sub string presence.

We can also ignore the casing using the String internal methods. To do this we will convert the Response in lower case and then compare it with our lower case string value. Below code demonstrates that.

The above two approaches suffer from a classical problem, what if the string “Hyderabad” is present in a wrong node or may be multiple instances of the same string are present. This is not a fool proof way of testing a particular node in the Response. There are better ways, Responseinterface gives you a mechanism to extract nodes based on a given JsonPath. There is a method called Response.JsonPath(), which returns a io.restassured.path.json.JsonPath Object. This object can be used to further query specific parts of the Response Json.

How to Extract a Node text from Response using JsonPath?

Let us continue with the above example and retrieve the Cityfrom the Response. To do so, we will simply get the JsonPath object from the Response interface and then query for the particular node. Just to be very clear, let us look at the Weather API response again.

1

2

3

4

5

6

7

8

{

"City":"Hyderabad",

"Temperature":"25.51 Degree celsius",

"Humidity":"94 Percent",

"Weather Description":"mist",

"Wind Speed":"1 Km per hour",

"Wind Direction degree":" Degree"

}

In this response, if we want to go to the City node, all we have to do is have the following JsonPath: $.City. Try it out on the JsonPath Evaluatorto verify the output.

Now let us look at the code, pay specific attention to the comments in the code.

Note: In Java JsonPath you do not need to have $ as the root node. You can completely skip that.

Author: Virender Singh

I am Virender Singh and I am a software Engineer.I have been in the Software profession for more than 12 years now. I worked on large spectrum of projects, from being a QA engineer to being a Development Engineer. At present I work as a Software Engineer for Microsoft India Development centre. I love to learn new technologies, specially in the field of Image Processing and Digital Signal processing. You can find me at following locations @LinkedIn@FaceBook@ToolsQA@Github

Have passed 11 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently I am working with BLOOMREACH as SDET.

I am passionate about designing Automation Frameworks that are effective and easy to maintain. For automating websites my weapons are QTP and Selenium (Webdriver). I live in Amsterdam(NL), with my wife and a lovely daughter.