Categories
Mastering Development

Java | Send post request json [closed]

I try to send POST request with JSON body.

I can understand what wrong.

update:
I run this code with GET request and its work fine.
i try to send POST request with body (json)
i saw example in web and I got an exception this cone not run
who can I send POST request that can work?

public class Program {
    public static void main(String[] args) {
        System.out.println("start");
        try {
            URL url = new URL ("https://<>.com/echo/post/json");
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setRequestProperty("Content-Type", "application/json; utf-8");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestMethod("POST");
            String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";

            OutputStreamWriter os = new OutputStreamWriter(con.getOutputStream());
            os.write(jsonInputString);


            System.out.println(con.getResponseCode());
        }
        catch (Exception e){
            System.out.println("some thing was wrong!!!!!!!");
        }



    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *