httpurlconnection-post
Making a JSON POST Request With HttpURLConnection
1. Overview
In this tutorial, we’ll demonstrate how to make a JSON POST request using HttpURLConnection.
2. Building a JSON POST Request with HttpURLConnection
Let’s create a URL object with the target URI string that accepts the JSON data via HTTP POST method:
URL url = new URL ("https://reqres.in/api/users");
2.2. Open a Connection
From the above URL object, we can invoke the openConnection method to get the HttpURLConnection object.
We can’t instantiate HttpURLConnection directly, as it’s an abstract class:
HttpURLConnection con = (HttpURLConnection)url.openConnection();
2.3. Set the Request Method
con.setRequestMethod("POST");
2.4. Set the Request Content-Type Header Parameter
Set “content-type” request header to “application/json” to send the request content in JSON form. This parameter has to be set to send the request body in JSON format.
Failing to do so, the server returns HTTP status code “400-bad request”.
con.setRequestProperty("Content-Type", "application/json; utf-8");
Also, note that we’ve mentioned charset encoding along with content type. This is useful if the request content encoding is different from UTF-8 encoding, which is the default encoding.
2.5. Set Response Format Type
con.setRequestProperty("Accept", "application/json");
2.6. Ensure the Connection Will Be Used to Send Content
Otherwise, we’ll not be able to write content to the connection output stream:
con.setDoOutput(true);
2.7. Create the Request Body
String jsonInputString = "{"name": "Upendra", "job": "Programmer"}";
We’d need to write it:
try(OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
2.8. Read the Response from Input Stream
Get the input stream to read the response content. Remember to use try-with-resources to close the response stream automatically.
Read through the whole response content, and print the final response string:
try(BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
}
3. Conclusion
As always, relevant code snippets can be found over on GitHub