Performing HTTP POST operation in JAVA

0 votes
Consider the following scenario:

A person wants to make a simple HTTP POST using JSON in Java.

Let's say the URL is

and it takes in the value {"name":"myname","age":"20"} labeled as 'details' for example.

How would he go about creating the syntax for the POST?

Also please point me to POST method in the JSON Javadocs.
May 31, 2018 in Java by developer_1
• 3,300 points

2 answers to this question.

0 votes

I would suggest the following two ways:

1. One is through the below mentioned code

HttpClient httpClient = new DefaultHttpClient();

try {
    HttpPost request = new HttpPost("http://yoururl");
    StringEntity params =new StringEntity("details={\"name\":\"myname\",\"age\":\"20\"} ");
    request.addHeader("content-type", "application/json");
    HttpResponse response = httpClient.execute(request);

    // handle response here...
}catch (Exception ex) {
    // handle exception here
} finally {

2. Also you can make use of Gson library to convert your java classes to JSON objects.

Create a pojo class for variables you want to send as per above Example



class pojo1
   String name;
   String age;
   //generate setter and getters

once you set the variables in pojo1 class you can send that using the following code

String       postUrl       = "";// put in your url
Gson         gson          = new Gson();
HttpClient   httpClient    = HttpClientBuilder.create().build();
HttpPost     post          = new HttpPost(postUrl);
StringEntity postingString = new StringEntity(gson.toJson(pojo1));//gson.tojson() converts your pojo to json
post.setHeader("Content-type", "application/json");
HttpResponse  response = httpClient.execute(post);

and these are the imports

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.HttpClientBuilder;

and for GSON

answered May 31, 2018 by Rishabh
• 3,580 points
0 votes

I'm using JSON-Java to build my JSON object:

JSONObject json = new JSONObject();
json.put("someKey", "someValue");    

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {
    HttpPost request = new HttpPost("http://yoururl");
    StringEntity params = new StringEntity(json.toString());
    request.addHeader("content-type", "application/json");
// handle response here...
} catch (Exception ex) {
    // handle exception here
} finally {
answered Nov 26, 2018 by Sushmita
• 6,890 points

Related Questions In Java

0 votes
1 answer

Working of post increment operator in Java

Well, I think the confusion is because ...READ MORE

answered May 23, 2018 in Java by geek.erkami
• 2,680 points
0 votes
1 answer

Simple HTTP server in Java using only Java SE API

You can write a pretty simple embedded Jetty Java ...READ MORE

answered Aug 28, 2018 in Java by Parth
• 4,640 points
0 votes
1 answer

How to encode the HTTP URL address in Java?

Use one of the constructors with more ...READ MORE

answered Oct 23, 2018 in Java by Sushmita
• 6,890 points
0 votes
1 answer

Http Basic Authentication in Java using HttpClient?

String encoding = Base64Encoder.encode ("test1:test1"); HttpPost httppost = ...READ MORE

answered Dec 20, 2018 in Java by Daisy
• 8,110 points
0 votes
1 answer

How to fire and handle HTTP requests

There are 2 options you can go ...READ MORE

answered Jun 13, 2018 in Java by Rishabh
• 3,580 points
0 votes
2 answers

Send HTTP request in Java

import; import; import; import; import; import; import ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,200 points
+1 vote
11 answers

How does Connection reset happen ?

You can use wireshark to view the ...READ MORE

answered Dec 7, 2018 in Java by tushh
0 votes
1 answer

Trusting all certificates using HttpClient over HTTPS

Note: Do not implement this in production ...READ MORE

answered Jun 25, 2018 in Java by Rishabh
• 3,580 points
+1 vote
11 answers

How to send HTTP POST requests on Java?

How to invoke Thread dump analysis API? Invoking ...READ MORE

answered Jun 16, 2019 in Java by Jim
• 810 points

reshown Jun 17, 2019 by Vardhan 74,654 views
0 votes
11 answers

Java - sending HTTP parameters via POST method easily

You can write like this simply get ...READ MORE

answered Jul 7 in Java by anonymous
• 140 points