I would like 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 I go about creating the syntax for the POST?
I also can't seem to find a POST method in the JSON Javadocs.
@momo's answer for Apache HttpClient, version 4.3.1 or later. I'm using
JSON-Java
to build my JSON object:It's probably easiest to use HttpURLConnection.
http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
You'll use JSONObject or whatever to construct your JSON, but not to handle the network; you need to serialize it and then pass it to an HttpURLConnection to POST.
You can use the following code with Apache HTTP:
Additionally you can create a json object and put in fields into the object like this