REST API automation testing using Apache HttpClient – The Approach

After the initial introduction to REST , its time for looking into techniques and tools , which can be used for automation testing of APIs based on REST.

What can be a better option rather than this – Something that can execute various HTTP request methods (PUT/GET etc etc.) , outputs response from server irrespective of whatever format it is, a flexibility to user to capture the response, extract out the needful and release the connection!

If we consider Java as our language to make it happen , to name a such library is –  Apache HttpClient


Well, there are several implementations of the request and response Content-Types(JSON, XML,HTML etc etc) , representations and all, which implements REST to design APIs. We are going to take example on – JSON request and response representations.

What are the requirements? – Tools/Libraries used

1. Apache HttpClient – A Java based library for HTTP communications
2. JSON parser for Java – we will be using org.json (It has APIs which really complements what JSON representations actually mean)
3. Eclipse
4. JUnit

Java JDK and some basic environmental set up etc are – Understood. 🙂

Basic idea behind combining these tools:

1. We will be creating instances of HttpClient for various methods like GET,PUT,POST etc. by mentioning the request URL

For example : We want to send a GET request to –
It supposed to provide some information about the domain:

So we will be creating instance of HttpGet and setting the URL as :

2. We will be composing the request body as JSON

As the above example is a GET request , so it can not have a request body. In case of POST, PUT etc if we need to set the request body then that needs to be represented in JSON format according to the design if the API. So we will be  extensively using org.json methods/APIs to build JSON objects and set it as HTTP request body.

3. Execute the desired HTTP method when headers etc are all set

As the request body contains request information in JSON format , so we need to set a header – Content-Type as ‘application/json’ , which gives instruction to server that request body is represented as JSON and should be parsed accordingly to process the request.

4. Capture the response and convert it to a JSON object.

Response object is captured and parsed to a JSON object.

5. Verify various Key- Value pairs along with the response code

Verify / assert the components in response – e.g ID , name and its values in the above response.

Objectives of Testing:

1. Verify Response code (200, 400 etc etc)
2. Verify Response Line (OK, SUCCESS etc etc)
3. Verify Presence of desired headers in response (application/json, image/png etc etc)
4. Verify presence of various Keys in response body
5. Verify values of Keys whenever we have expected values in hand
6. Verify if response is in JSON Array, JSON string etc etc.

Let’s have a look into how to combine these Java libraries for effective testing of REST APIs (From coding point of view) in next few blogs.

Happy Testing!!

  • Siva

    Hi Abhishek Thanks for the post

    I am looking for JSon automation + Test NG. can you please provide sample project which you have done pls

  • Nikhil Shetty

    I am also looking for JSon automation + Test NG. can you please provide sample project which you have done pls

  • Pritam Samantaray

    even i need the same thing.
    can you pls give information how to implement java,selenium and testng for api automation

  • susithra sampath


    I’m a beginner to API testing. Can you please share the sample project? to

  • Girish Mohan

    where are the remaining posts dude

  • palani

    i am also want JSon automation + Test NG , if you have any code please share me.