Authentication is the process or action of verifying the identity of a user or process. Apache HttpClient has the capability to test the authentication mechanisms with ease and that is what we are going to see and learn in this tutorial. In this example, we will learn “How to perform Basic Authentication using Apache HttpClient”.

Basic_Authentication_HttpClient_Techndeck

Let’s understand the authentication a bit, In order to login into an email account, you need to provide a username and password in order to prove your authenticity that whether you are a valid user or not.

There are various types of authentication mechanisms are available like Basic Authentication, API Keys, OAuth. In this particular example, we are going to use Basic Authentication mechanism.

Simplest example to understand Basic Authentication mechanism using Apache HttpClient...!!! Click To Tweet

In this tutorial, we are going to cover below topics:

  1. What is Basic Authentication?
  2. How to make a GET Request to the resource that requires Username/Password to authenticate?

 

Check out: BASIC AUTHENTICATION using another popular API testing Framework – REST ASSURED

Let’s begin:

1. What is Basic Authentication?

In this method of authentication, a username and password should be provided by the USER agent to prove their authentication. It’s a straight forward and simple approach which basically uses HTTP header with “username and password” encoded in base64. It does not require cookies, session IDs etc.

2. How to make a GET Request to the resource that requires Username/Password to authenticate?

In order to test this feature, we are going to use a Dummy Rest API available online for testing purpose at http://httpbin.org. This website expose some sample rest services. 

A brief description about the web service that we are going to test:

This service provides authenticity if supplied by valid username and password.

1. Request Endpoint : GET

2. Authentication Information :

Valid Username : user

Valid Password : passwd

3. Service Endpoint URL : ‘http://httpbin.org/basic-auth/user/passwd’

4. Expected Response :

Now, let’s look at the Java code to perform Basic Authentication using HttpClient:

 

Let’s try to understand the code:

1. Specify the URL and Credentials

 

2. Create an object of BasicCredentialsProvider class which extends CredentialsProvider interface whose job is to maintain the collection of user credentials

 

3. Set up the CloseableHttpClient object and customize it by supplying the appropriate credentials

 

4. Create a basic Get Request using HttpRequest object and pass the resource URI to it

 

5. Submit the Request using HttpGet -> Execute method

 

6. Create a BufferedReader object and store the raw Response content into it.

 

7. Throw runtime exception if status code isn’t 200

 

8. Create the StringBuffer object and store the response into it.

 

Eclipse Console Output:

 

 

That’s it, it’s that simple to perform Basic Authentication using Apache HttpClient: ?

Simplest example to understand Basic Authentication mechanism using Apache HttpClient...!!! Click To Tweet

 

If you like this post, please click like button and share it with others on Twitter. Also, check out my other useful blog posts on Apache HttpClient:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.