Implementation of java POST request in java

 avatar
user_9237437122
java
3 years ago
1.1 kB
4
Indexable
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
/**
 * @author Anik
 */
public class PostReques {

         public static void main(String[] args) throws MalformedURLException,IOException {       
        URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
        http.setRequestMethod("POST");
        http.setDoOutput(true);
        http.setRequestProperty("Authorization", "Bearer QW1hbnVyIFJhaG1hbiBBbWFu");
        http.setRequestProperty("Content-Type", "application/json");
        String data = "{\n    \"id\": \"192002118\",\n}";
        byte[] out = data.getBytes(StandardCharsets.UTF_8);
        OutputStream stream = http.getOutputStream();
        stream.write(out);
        System.out.println("Response Code: " +http.getResponseCode() + " " +"Response Message: "+ http.getResponseMessage());
        http.disconnect();
    }  
    
}
Editor is loading...