Consume Response Entity From Http Response Example - Apache Http Client Examples

#JavaInspires

Consume Response Entity From Http Response - Apache Http Client Examples



Here is the example code to execute simple Http POST request using Apache Http Client library.

we used:
Java 8, Eclipse IDE, Maven, Apache Http Client 4.5.7


Note: Here we have "reqres" publicly available api, you can use any REST api or use the api which you want to consume.


Project Folder Structure
-------------------------
pom.xml
---------

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>javainspires</groupId>
 <artifactId>apachehttpclientexamples</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>apachehttpclientexamples</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
  <dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>4.5.7</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
  <dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpcore</artifactId>
   <version>4.4.11</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  <dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.2</version>
  </dependency>
 </dependencies>
</project>



ConsumeResponseEntityDemo.java
----------------------------------------------------------
package javainspires.apachehttpclientexamples;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 
 * @author javainspires
 *
 */
public class ConsumeResponseEntityDemo {

 public static void main(String[] args) {

  try {
   // Http Get Request
   // create http client object
   HttpClient httpClient = HttpClients.createDefault();

   // create http GET request
   HttpGet httpGetRequest = new HttpGet();

   // set request URI to the created request object
   httpGetRequest.setURI(new URI("https://reqres.in/api/users/2"));

   // execute request using httpclient object
   HttpResponse httpResponse = httpClient.execute(httpGetRequest);

   // get Http Entity from response
   HttpEntity httpEntity = httpResponse.getEntity();

   // check for null
   if (httpEntity != null) {
    // convert httpentity into string
    String responseBody = EntityUtils.toString(httpEntity);
    // print response body
    System.out.println("Response Body ::: \n" + responseBody);
   }
  } catch (URISyntaxException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ClientProtocolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}


Output:
-------------
Response Body ::: 
{"data":{"id":2,"first_name":"Janet","last_name":"Weaver","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"}}





Post a Comment

Previous Post Next Post