【发布时间】: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 中尝试过,但请尝试一下。 -
您好,请检查我的答案,如果对您有帮助