Simple POST Request Example - Apache Http Client Examples

#JavaInspires

Simple POST Request Example 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>



SimpleHttpPOSTRequestDemo.java
-----------------------------------------------------------
package javainspires.apachehttpclientexamples;

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

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;

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

 public static void main(String[] args) {
  try {
   // Http POST Request

   // create http client object
   HttpClient httpClient = HttpClients.createDefault();

   // create http POST request
   HttpPost httpPost = new HttpPost();

   // set request URI ro thr created request object
   httpPost.setURI(new URI("https://reqres.in/api/users"));

   // construct JSON body
   String requestBody = "{    \"name\": \"morpheus\",    \"job\": \"leader\"}";

   // convert request body into string entity

   StringEntity stringEntity = new StringEntity(requestBody);

   // set stringEntity to the created post request
   httpPost.setEntity(stringEntity);

   // execute created httpPost request
   HttpResponse httpResponse = httpClient.execute(httpPost);

   System.out.println("Status Code - " + httpResponse.getStatusLine().toString());

  } catch (URISyntaxException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (UnsupportedEncodingException 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:
-------------

Status Code - HTTP/1.1 201 Created





Post a Comment

Previous Post Next Post