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.
Here is what you need to do:
The code roughly looks like (you will still need to debug it and make it work)
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
becomes
once you set the variables in pojo1 class you can send that using the following code
and these are the imports
and for GSON
I recomend http-request built on apache http api.
If you want send
JSON
as request body you can:I higly recomend read documentation before use.
I found this question looking for solution about how to send post request from java client to Google Endpoints. Above answers, very likely correct, but not work in case of Google Endpoints.
Solution for Google Endpoints.
Content type header must be set to "application/json".
It sure can be done using HttpClient as well.
Try this code: