태그 보관물: http-status-code-415

http-status-code-415

JSON에서 HTTP 415 지원되지 않는 미디어 유형 오류 개체를 포함하지

JSON 요청으로 REST 서비스를 호출하고 있는데 HTTP 415 "Unsupported Media Type"오류로 응답합니다 .

요청 콘텐츠 유형은로 설정됩니다 ("Content-Type", "application/json; charset=utf8").

요청에 JSON 개체를 포함하지 않으면 제대로 작동합니다. google-gson-2.2.4JSON 용 라이브러리를 사용하고 있습니다.

몇 가지 다른 라이브러리를 사용해 보았지만 아무런 차이가 없었습니다.

아무도이 문제를 해결하도록 도와 주시겠습니까?

내 코드는 다음과 같습니다.

public static void main(String[] args) throws Exception
{

    JsonObject requestJson = new JsonObject();
    String url = "xxx";

    //method call for generating json

    requestJson = generateJSON();
    URL myurl = new URL(url);
    HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
    con.setDoOutput(true);
    con.setDoInput(true);

    con.setRequestProperty("Content-Type", "application/json; charset=utf8");
    con.setRequestProperty("Accept", "application/json");
    con.setRequestProperty("Method", "POST");
    OutputStream os = con.getOutputStream();
    os.write(requestJson.toString().getBytes("UTF-8"));
    os.close();


    StringBuilder sb = new StringBuilder();
    int HttpResult =con.getResponseCode();
    if(HttpResult ==HttpURLConnection.HTTP_OK){
    BufferedReader br = new BufferedReader(new   InputStreamReader(con.getInputStream(),"utf-8"));

        String line = null;
        while ((line = br.readLine()) != null) {
        sb.append(line + "\n");
        }
         br.close();
         System.out.println(""+sb.toString());

    }else{
        System.out.println(con.getResponseCode());
        System.out.println(con.getResponseMessage());
    }

}
public static JsonObject generateJSON () throws MalformedURLException

{
   String s = "http://www.example.com";
        s.replaceAll("/", "\\/");
    JsonObject reqparam=new JsonObject();
    reqparam.addProperty("type", "arl");
    reqparam.addProperty("action", "remove");
    reqparam.addProperty("domain", "staging");
    reqparam.addProperty("objects", s);
    return reqparam;

}
}

의 값 requestJson.toString()은 다음과 같습니다.

{"type":"arl","action":"remove","domain":"staging","objects":"http://www.example.com"}



답변

이유는 확실하지 않지만 줄 charset=utf8을 제거 con.setRequestProperty("Content-Type", "application/json; charset=utf8")하면 문제 가 해결되었습니다.


답변

추가 Content-Type: application/jsonAccept:application/json


답변

charset=utf8뒤에 공백이 없어야 하기 때문 application/json입니다. 잘 작동합니다. 그것을 사용하십시오application/json;charset=utf-8


답변

jquery ajax 요청을하는 경우 추가하는 것을 잊지 마십시오.

contentType:'application/json'


답변

사용하는 경우 AJAX jQueryRequest 신청해야합니다. 그렇지 않으면 415오류가 발생합니다.

dataType: "json",
contentType:'application/json'


답변

HTTP 헤더 관리자를 추가하고 API의 헤더 이름과 값을 추가합니다. 예 : 콘텐츠 유형, 수락 등. 그러면 문제가 해결됩니다.


답변

React RSAA 미들웨어 또는 이와 유사한 경우에 헤더를 추가하십시오.

  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(model),