Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll demonstrate how to make a JSON POST request using HttpURLConnection.

Further reading:

Do a Simple HTTP Request in Java

A quick and practical guide to performing basic HTTP requests using Java's built-in HttpUrlConnection.

Authentication with HttpUrlConnection

Learn how to authenticate HTTP requests using HttpUrlConnection.

Connecting Through Proxy Servers in Core Java

Learn how to connect to proxy servers in Java using system properties or the more flexible Proxy class.

2. Building a JSON POST Request With HttpURLConnection

2.1. Create a URL Object

Let’s create a URL object with a 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

To send a POST request, we’ll have to set the request method property to POST:

con.setRequestMethod("POST");

2.4. Set the Request Content-Type Header Parameter

Set the “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");

2.5. Set Response Format Type

Set the “Accept” request header to “application/json” to read the response in the desired format:

con.setRequestProperty("Accept", "application/json");

2.6. Ensure the Connection Will Be Used to Send Content

To send request content, let’s enable the URLConnection object’s doOutput property to true.

Otherwise, we won’t be able to write content to the connection output stream:

con.setDoOutput(true);

2.7. Create the Request Body

After creating a custom JSON String:

String jsonInputString = "{"name": "Upendra", "job": "Programmer"}";

We would 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());
}

If the response is in JSON format, use any third-party JSON parsers such as Jackson library, Gson, or org.json to parse the response.

3. Conclusion

In this article, we learned how to make a POST request with JSON content body using HttpURLConnection.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW