[JAVA] java.io.IOException: Server returned HTTP response code: 415 for URL

[JAVA] java.io.IOException: Server returned HTTP response code: 415 for URL

HttpURLConnection 객체 사용 시 java.io.IOException: Server returned HTTP response code: 415 for URL: https://도메인명 오류가 발생하는 경우.

만약 HTTP POST 메서드를 사용해서 JSON 데이터를 보내려고 한다면, “Content-Type: application/x-www-form-urlencoded” 가 아닌 “Content-Type: application/json” 을 헤더에 적용해야 한다.

[AS-IS]

HttpURLConnection ucon = null;

    try {
        URL url = new URL(urlString);

        ucon = (HttpURLConnection) url.openConnection();

        ucon.setRequestMethod(“POST”);

(후략)

[TO-BE]

HttpURLConnection ucon = null;

    try {
        URL url = new URL(urlString);

        ucon = (HttpURLConnection) url.openConnection();

        ucon.setRequestMethod(“POST”);

        ucon.setRequestProperty(“Content-Type”, “application/json”);

(후략)

참고사이트 : https://pythonq.com/so/java/267460