【问题标题】:R: httr SOAP POST request for xml return error - "media type is unsupported"R:用于 xml 的 httr SOAP POST 请求返回错误 - “不支持媒体类型”
【发布时间】:2020-01-29 21:19:48
【问题描述】:

我正在尝试使用 R 脚本进行 SOAP 查询。期待 xml 文件响应。 POST 请求返回 415 错误。我正在努力如何在 R 中正确表达它。

我在 httr 中尝试过:

library(httr)
library(xml2)
library(RCurl)


headers = "{'content-type': 'text/xml'}"  #Also tried'application/xml; charset=utf-8'}"
body = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetGenericData xmlns="http://...">
      <QueryMessage xmlns="http:.../...">
        <Query>
          <DataWhere xmlns="http://.../...">
            <And>
              <DataSet>ABC</DataSet>
              <Dimension id="ABC">abc</Dimension>
            </And>
          </DataWhere>
        </Query>
      </QueryMessage>
    </GetGenericData>
  </soap:Body>
</soap:Envelope>'

#headers <- c('application/xml; charset=utf-8')
#names(headers) <- 'content-type'

response = POST("http://exampleWebService?op=GetGenericData", 
                config = list(add_headers(.headers = headers)) ,
                body = body, 
                content_type(".xml"),
                encode = 'raw',
                accept_xml(),
                type = 'application/soap+xml; charset=utf-8',
                # content_type_xml(),
                authenticate(":", ":", "ntlm"))
                )

那么查询响应状态为错误:

> response[["all_headers"]][[1]]
$status
[1] 401

> response[["all_headers"]][[2]]
$status
[1] 415

> http_type(response)
[1] "text/html"
> http_error(response)
[1] TRUE

>content(response)

No encoding supplied: defaulting to UTF-8.
{xml_document}
<html>
[1] <body><p>The server cannot service the request because the media type is unsupported.</p></body>

无论我如何更改标题,http_type(response) 将始终返回[1] "text/html"。看来我的客户端请求在服务器端没有正确处理? 什么是正确的标题/编码/内容类型???有什么提示吗?


【问题讨论】:

  • 正文 xml 内容的措辞是否正确?三引号似乎没有必要,但如果我在 body 变量周围加上三引号,响应类型将变为“application/xml +soap”
  • 您需要为我们提供一个我们可以使用的服务器并尝试修复错误。如果我们无法重现您遇到的错误,我们将无能为力。谢谢。
  • 这与我在集成 susiAir 肥皂请求时遇到的 java media type is unsupported 中的错误相同。我已经通过像这样"text/xml; charset=utf-8" 传递标题来解决。我没有在 R 中尝试过,但请尝试一下。
  • 您好,请检查我的答案,如果对您有帮助

标签: r soap rcurl httr


【解决方案1】:

1也许它的accept_xml()Accept:标头默认是application/xml试试accept("application/soap+xml")

2 或将上下文类型更改为 text/plain 以查看您的服务器是否设置为处理 xml MIME 或看不到响应是 200 还是 415 那么您就会知道问题出在哪里响应或客户请求

3 可能标头在您的服务器中Content-Type: application/soap+xml; charset=utf-8 区分大小写

【讨论】:

  • 他们没有工作...... :(我从'txt/plain'到'text/xml'或''application/soap+xml'来回切换,错误仍然存​​在。如何解释 response[["all_headers"]][[1]] $status [1] 401 VS > response[["all_headers"]][[2]] $status [1] 415?为什么它说两个“未经授权”和错误的媒体类型?
  • 嗨,请先尝试在您的代码中发送具有相同标题的 EMPTY XML BODY,也尝试在您的代码中发送具有相同标题的 EMPTY BODY,尝试在我的答案中使用 EMPTY 正文的标题,这可能会给出答案在服务器上它接受哪些标头以及哪些标头是正确的。
猜你喜欢
  • 2018-10-13
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-17
  • 2019-07-06
  • 1970-01-01
  • 2016-11-26
相关资源
最近更新 更多