Skip to content
the poor coder

httpclient post raw json body

Poor Man
Published
Reading time
2 min read
On this page

How to make a HTTP POST request with raw JSON body using HttpClient in Java?#

If you want to make a HTTP POST request with raw JSON body using HttpClient in Java, you can do this in several ways:

Method 1: Using StringEntity#

One way is to use StringEntity class provided by HttpClient. Here is the code:

Text

  // Create HttpClient instance
  HttpClient client = HttpClientBuilder.create().build();

  // Set URL
  HttpPost httpPost = new HttpPost("http://example.com/api/resource");

  // Set headers
  httpPost.setHeader("Content-Type", "application/json");

  // Set request body
  String jsonBody = "{\"key\": \"value\"}";
  StringEntity entity = new StringEntity(jsonBody);
  httpPost.setEntity(entity);

  // Execute HTTP POST request
  HttpResponse response = client.execute(httpPost);
  

Method 2: Using JSONObject#

Another way is to use JSONObject class from JSON library. Here is the code:

Text

  // Create HttpClient instance
  HttpClient client = HttpClientBuilder.create().build();

  // Set URL
  HttpPost httpPost = new HttpPost("http://example.com/api/resource");

  // Set headers
  httpPost.setHeader("Content-Type", "application/json");

  // Set request body
  JSONObject jsonBody = new JSONObject();
  jsonBody.put("key", "value");
  StringEntity entity = new StringEntity(jsonBody.toString());
  httpPost.setEntity(entity);

  // Execute HTTP POST request
  HttpResponse response = client.execute(httpPost);
  

Method 3: Using Gson#

A third way is to use Gson library which provides a convenient way to serialize Java objects to JSON and vice versa. Here is the code:

Text

  // Create HttpClient instance
  HttpClient client = HttpClientBuilder.create().build();

  // Set URL
  HttpPost httpPost = new HttpPost("http://example.com/api/resource");

  // Set headers
  httpPost.setHeader("Content-Type", "application/json");

  // Set request body
  Gson gson = new Gson();
  Map<String, String> jsonBody = new HashMap<>();
  jsonBody.put("key", "value");
  String json = gson.toJson(jsonBody);
  StringEntity entity = new StringEntity(json);
  httpPost.setEntity(entity);

  // Execute HTTP POST request
  HttpResponse response = client.execute(httpPost);
  

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC