POST request send JSON data Java HttpUrlConnection

POST request send JSON data Java HttpUrlConnection

To send a POST request with JSON data in Java using HttpURLConnection, you can follow these steps:

  1. Create a JSON payload (string) containing the data you want to send.
  2. Set up the HttpURLConnection to make a POST request.
  3. Set the request method to "POST."
  4. Set the "Content-Type" header to "application/json" to indicate that you are sending JSON data.
  5. Write the JSON payload to the request's output stream.
  6. Send the request and read the response if necessary.

Here's a code example that demonstrates how to do this:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpPostJsonExample { public static void main(String[] args) { try { // JSON data to be sent in the POST request String jsonInputString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; // URL of the API endpoint where you want to send the POST request String apiUrl = "https://example.com/api/endpoint"; // Create a URL object with the API URL URL url = new URL(apiUrl); // Open a connection to the URL HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Set the request method to POST connection.setRequestMethod("POST"); // Set the request headers connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept", "application/json"); // Enable input and output streams connection.setDoOutput(true); // Write the JSON data to the request's output stream try (OutputStream os = connection.getOutputStream()) { byte[] input = jsonInputString.getBytes("utf-8"); os.write(input, 0, input.length); } // Get the HTTP response code int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); // Read the response (if needed) if (responseCode == HttpURLConnection.HTTP_OK) { try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) { StringBuilder response = new StringBuilder(); String responseLine; while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } System.out.println("Response: " + response.toString()); } } else { System.out.println("POST request failed."); } // Close the connection connection.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } 

In this example:

  • We create a JSON string called jsonInputString that contains the data you want to send in the POST request.

  • We specify the URL of the API endpoint where the POST request will be sent (replace "https://example.com/api/endpoint" with your actual API endpoint).

  • We set up the HttpURLConnection to make a POST request and set the "Content-Type" header to "application/json" to indicate that we are sending JSON data.

  • We write the JSON data to the request's output stream using an OutputStream.

  • We send the request and read the response (if needed).

Make sure to handle exceptions and error cases as appropriate for your application.


More Tags

identity-column python-multithreading networkimageview jekyll aws-lambda multi-level nodemailer subscriptions predict gradle-eclipse

More Java Questions

More Fitness-Health Calculators

More Housing Building Calculators

More Fitness Calculators

More Chemistry Calculators