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 www.site.com

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,320 points
4,177 views

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");
    request.addHeader("Accept","application/json");
    request.setEntity(params);
    HttpResponse response = httpClient.execute(request);

    // handle response here...
}catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.getConnectionManager().shutdown();
}

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

{"name":"myname","age":"20"}

becomes

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       = "www.site.com";// 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.setEntity(postingString);
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

import com.google.gson.Gson;
answered May 31, 2018 by Rishabh
• 3,620 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");
    request.setEntity(params);
    httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.close();
}
answered Nov 26, 2018 by Sushmita
• 6,910 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
553 views
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 29, 2018 in Java by Parth
• 4,630 points
1,613 views
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,910 points
2,868 views
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,120 points
3,568 views
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,620 points
920 views
0 votes
2 answers

Send HTTP request in Java

import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpResponse; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import java.io.IOException; import ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,220 points
1,482 views
+1 vote
13 answers

How does java.net.SocketException: Connection reset happen ?

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

answered Dec 7, 2018 in Java by tushh
248,342 views
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,620 points
10,937 views
+1 vote
13 answers

How to send HTTP POST requests on Java?

With Apache HttpClient In the old days, this Apache ...READ MORE

answered Dec 10, 2020 in Java by Rajiv
• 8,910 points
157,132 views
0 votes
12 answers

Java - sending HTTP parameters via POST method easily

Below are the steps we need to ...READ MORE

answered Dec 11, 2020 in Java by Rajiv
• 8,910 points
109,094 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP