【问题标题】:415 Unsupported Media Type for XML Post Rest API415 XML Post Rest API 的媒体类型不受支持
【发布时间】:2015-06-02 00:19:19
【问题描述】:

我正在尝试使用 XML 字符串请求 REST POST API,但我收到以下错误响应。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<apiErrors>    
   <apiError>        
      <errorMessage>415 Unsupported Media Type - null</errorMessage>    
   </apiError>
</apiErrors>

我不明白为什么会这样。即使我也设置了内容类型和标题。

请看一下我的代码。

String XmlString = "<Contact>"
                                +"<name>Sandeep</name>"
                                +"<title>Mr.</title>"
                                +"<Address>India</Address>"                                 
                                +"</Contact>";      

            try {
                StringEntity se = new StringEntity(XmlString);
                HttpPost httpRequest = new HttpPost("http://abc:xyz123@000.000.000.00:8080/aaa/sample/feed/000001");
                httpRequest.setHeader("Content-Type","application/soap-xml;charset=UTF-8");
                httpRequest.setHeader("Accept","text/plain");

                httpRequest.setEntity(se);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse httpResponse;
                httpResponse = httpclient.execute(httpRequest, new BasicHttpContext());

                finalres = inputStreamToString(httpResponse.getEntity().getContent())
                            .toString();

请给我提示或参考。

【问题讨论】:

  • 能发下相关网址吗?
  • 尝试使用 curl 发送您的请求(这是一个 linux 实用程序,google for man curl)。顺便说一句,我在 Accept 中看到 text/plain,你不是说 XML 吗?
  • 你能说一下服务器REST实现的技术是什么吗?你确定服务器应该接受 application/soap-xml 而不是 application/xml?

标签: android rest post


【解决方案1】:

你试过不使用 charset="utf-8" 吗?

(由于我的声誉低,我无法发表评论)

【讨论】:

    【解决方案2】:

    在您的请求中,您将通信指定为soap-xml

    httpRequest.setHeader("Content-Type","application/soap-xml;charset=UTF-8");
    

    尝试通过删除“soap”部分来指定使用纯 xml。

    有效的 xml 内容类型:

    “文本/xml”或“应用程序/xml”

    Soap 调用是非常特别格式化的 xml,并且从响应中,除非您修剪输出以删除所有的 soap xml,否则它似乎是一个普通的 xml 服务。

    【讨论】:

      【解决方案3】:

      如果您在 REST 服务中使用 @Consumes(MediaType.APPLICATION_XML),则使用如下内容类型

      httpRequest.setHeader("Content-Type","application/xml;");
      

      使用浏览器 REST 客户端(例如 Curl、Advanced rest clent 等)检查您的应用程序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-17
        • 2018-06-11
        • 1970-01-01
        • 2016-11-26
        • 2019-01-16
        • 2020-09-03
        • 2017-03-06
        • 1970-01-01
        相关资源
        最近更新 更多