• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Web Services Testing / How to perform Web Services Testing using HTTPClient

How to perform Web Services Testing using HTTPClient

May 19, 2015 by Mukesh Otwani 25 Comments

Web Services Testing

Web Services Testing

Download – HTTP Client  from below link and extract the zip file and add all jars into project

http://hc.apache.org/downloads.cgi 

Add

JSON Jar to read json format.

 

Web Services Testing

 

Before moving to test Web Services Testing, please go through the basics of Web Service.

Please check below article from W3School

Intro to WebServices

We will be testing Web Services in two ways

1- Using JSON File.

2-Using XML File.

 

How to verify HTTP Response for Web Service Testing

First we will verify HTTP response using response code below

Status codes

Most status codes which frequently used

200- Ok

404- Page not found

401- Unauthorized

 

Below is the piece of code which will verify HTTP Response

public static void testStatusCode(String restURL) throws ClientProtocolException, IOException

{
// Create Object and pass the url
HttpUriRequest request = new HttpGet(restURL);

// send the response or execute the request
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

// Verify the response code is equal to 200
Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(),HttpStatus.SC_OK);
}

 

How to verify the response Content type for Web Services testing

public static void testMimeType(String restURL, String expectedMimeType) throws ClientProtocolException, IOException {

// Create object of HTTP request
HttpUriRequest request = new HttpGet(restURL);

// Send the request 
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

// Verify the response type 
Assert.assertEquals(expectedMimeType,ContentType.getOrDefault(httpResponse.getEntity()).getMimeType());
}

 

How to extract response from XML File for Web Services Testing

public static void testContent(String restURL, String element, String expectedValue) throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {

// Parse the URL
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(restURL);

// Get the element from response using element tag name
NodeList nodelist = doc.getElementsByTagName(element);

// Verify the response content using Assert
Assert.assertEquals(expectedValue,nodelist.item(0).getTextContent()); 
}

 

How to extract response from JSON File for Web Services Testing

public static void testContentJSON(String restURL, String element, String expectedValue) throws ClientProtocolException, IOException, SAXException, ParserConfigurationException, JSONException {

// Create request object
HttpUriRequest request = new HttpGet(restURL);

// send response or execute query
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

// Convert the response to a String format
String result = EntityUtils.toString(httpResponse.getEntity());

// Convert the result as a String to a JSON object
JSONObject jo = new JSONObject(result);

// Verify content
Assert.assertEquals(expectedValue, jo.getString(element));

}

 

Full program for Web Services Testing

import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class RESTTester {

public static void main (String args[]) {

// This is request which we are sending to server 
String restURL_XML = "http://parabank.parasoft.com/parabank/services/bank/customers/12212/";

// sending request and we are passing parameter in url itself
String restURL_JSON = "http://api.openweathermap.org/data/2.5/weather?q=Amsterdam";

try {

testStatusCode(restURL_XML);
testStatusCode(restURL_JSON);
testMimeType(restURL_XML,"application/xml");
testMimeType(restURL_JSON,"application/json");
testContent(restURL_XML,"lastName","Smith");
testContentJSON(restURL_JSON,"name","Amsterdam");

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void testStatusCode(String restURL) throws ClientProtocolException, IOException {

HttpUriRequest request = new HttpGet(restURL);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(),HttpStatus.SC_OK);
}

public static void testMimeType(String restURL, String expectedMimeType) throws ClientProtocolException, IOException {

HttpUriRequest request = new HttpGet(restURL);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

Assert.assertEquals(expectedMimeType,ContentType.getOrDefault(httpResponse.getEntity()).getMimeType());
}

public static void testContent(String restURL, String element, String expectedValue) throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {


Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(restURL);
NodeList nodelist = doc.getElementsByTagName(element);

Assert.assertEquals(expectedValue,nodelist.item(0).getTextContent()); 
}

public static void testContentJSON(String restURL, String element, String expectedValue) throws ClientProtocolException, IOException, SAXException, ParserConfigurationException, JSONException {

HttpUriRequest request = new HttpGet(restURL);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

// Convert the response to a String format
String result = EntityUtils.toString(httpResponse.getEntity());

// Convert the result as a String to a JSON object
JSONObject jo = new JSONObject(result);

Assert.assertEquals(expectedValue, jo.getString(element));

}

}

 

If you find this useful then please share with friends. Comment below if any doubt, suggestion or feedback.

Thanks for visiting my blog. Have a nice day 🙂

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

 

 

Filed Under: Web Services Testing Tagged With: API Testing, Web Services testing

Reader Interactions

Comments

  1. Vamshi G says

    May 11, 2019 at 12:18 AM

    Simple and easily explained. Thank you

    Reply
    • Mukesh Otwani says

      May 11, 2019 at 8:18 AM

      Thanks Vamshi…:)

      Reply
  2. Preetish Kumar Mahato says

    November 8, 2016 at 4:10 PM

    hi mukesh can u make a video for this tutorial… It will be easy to understand…

    Reply
  3. Fayaz says

    November 1, 2016 at 4:29 PM

    Hi Mukesh,

    Do we have any api where we can integrate wsdl and generate request using Java and Pass parameters dynamically for the same service.

    Reply
    • Mukesh Otwani says

      November 5, 2016 at 11:00 PM

      Hi Fayaz,

      Yes i have to cover this will cover soon.

      Reply
  4. shreesh pandey says

    October 15, 2016 at 11:48 PM

    Hi Mukesh can you plz provide video for this web services testing actually by looking at code i am little bit confused. Please do the needful for me.

    Reply
    • Mukesh Otwani says

      October 16, 2016 at 12:47 PM

      Yes will upload soon.

      Reply
  5. kanchana says

    September 23, 2016 at 4:50 AM

    Thank you mukesh ji. The code is very helpful. But if you give video explanation, it will be very beneficial for beginner levels. Hope Waiting the for the videos on Api testing.

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 11:19 PM

      Hi Kanchana,

      Yes Hope above code worked for you. I will try to create video on this soon and will update.

      Reply
  6. Abhishek says

    September 21, 2016 at 1:15 PM

    HEy… I want to develop a java famework to test rest apis. Do you have any course for this. I need it as a part of one project in my comapny

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 11:33 PM

      Hi Abhishek,

      No as of now only Selenium course is in place.

      Reply
  7. nandini says

    September 6, 2016 at 6:00 PM

    Hi Mukesh,

    The blog is very good & informative.

    I am at beginner level,I understood code & flow mentioned in this blog however it would be helpful if you could post the outpost as well

    Reply
    • Mukesh Otwani says

      September 7, 2016 at 11:51 PM

      Hi Nandini,

      sure will do that

      Reply
  8. cypherjobin says

    April 12, 2016 at 9:05 PM

    Dear Mukesh,
    Thanks a lot for the introduction to the httpclient and the httpcore.

    looks like there are slight modification is already happened with the way we need to access the JSON API => http://openweathermap.org/ . They have introduced the appid (session id) to establish the connection and then invoke their REST service. The call without the session id is ending with the 403 error.

    However the explanation and the code are really good to deep dive in to the API consumption. I can now play around with the XMLs and JSONs

    Thanks again for the explanation.

    Best Regards
    Jobin

    Reply
    • Mukesh Otwani says

      April 13, 2016 at 10:50 PM

      Thanks Jobin will update the content soon.

      Reply
  9. Ranjit says

    March 8, 2016 at 11:44 AM

    it’s good

    Reply
    • Mukesh Otwani says

      March 11, 2016 at 6:43 PM

      Thanks Ranjit

      Reply
  10. amit j says

    February 16, 2016 at 6:23 AM

    Very information. Thanks Mukesh.

    Reply
    • Mukesh Otwani says

      February 22, 2016 at 5:20 PM

      Thank you Amit keep visiting 🙂

      Reply
  11. venu says

    February 2, 2016 at 3:53 AM

    nice work on ws automation and keep doing great work. appreciate if you can upload Soap WS automation using java

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:22 PM

      Hi Venu,

      Thank you I have uploaded 3 videos on SOAP UI. Hope you like it.

      Reply
  12. Ramani says

    December 31, 2015 at 4:27 AM

    Hi Mukesh, this blog is useful. If you can upload a video on “How to perform Web Services Testing using HTTPClient” that would be great!

    Reply
    • Mukesh Otwani says

      January 2, 2016 at 10:49 PM

      Hi Ramani,

      Thank you keep visiting.

      Yes I will upload soon.

      Reply
  13. haritha says

    November 19, 2015 at 1:21 AM

    thank u .information is useful me good explanation

    Reply
    • Mukesh Otwani says

      November 19, 2015 at 1:16 PM

      Thank you Haritha 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?