Implementation of POST request

program to Implementation of POST request
 avatar
user_9332925630
java
2 years ago
1.2 kB
7
Indexable
Never
//Implementation of POST request
package httpreq;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
/**
 * @author aman
 */
public class PostRequest {
    public static void main(String[] args) throws MalformedURLException,IOException {       
        URL url = new URL("https://ar-aman.herokuapp.com/users");
        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    \"name\": \"Amanur Rahman\",\n  \"student_id\": \"192002117\", \n \"email\": \"aramancse@gmail.com\",\n \"varsity\": \"Green University of Bangladesh\"\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();
    }
}