【问题标题】:POST request in R: error in upload_file() with xmlR中的POST请求:带有xml的upload_file()中的错误
【发布时间】:2020-04-16 20:00:56
【问题描述】:

我正在尝试创建一个 POST 请求,但 body 参数未按预期工作。

POST_bodyRequest.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
 <rs:alarm-request throttlesize="0"
 xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">
 <rs:requested-attribute id="0x10000"/> 
 <rs:requested-attribute id="0x10001"/> 
 <rs:requested-attribute id="0x10009"/> 
 <rs:requested-attribute id="0x1000a"/> 
 <rs:requested-attribute id="0x1006e"/> 
 <rs:requested-attribute id="0x11ee8"/> 
</rs:alarm-request>

代码,基本上是 POST 调用

xml <- upload_file("POST_bodyRequest.xml")
r2 <- POST(url, login.password, body = list(xml))
status_code(r2)

首先要注意的是文件中的内容没有保存在“xml”文件中:

> xml <- upload_file("POST_bodyRequest.xml")
> xml
Form file: POST_bodyRequest.xml (type: application/xml) 
> str(xml)
List of 2
 $ path: chr "D:\\MPM\\POST_bodyRequest.xml"
 $ type: chr "application/xml"
 - attr(*, "class")= chr "form_file"

因此,POST 调用返回错误

> r2 <- POST(url, login.password, body = list(xml))
Error: All components of body must be named
> status_code(r2)
[1] 415

我也尝试过使用 xmlParse() 读取 xml 文件。在这种情况下,代码按预期恢复,但调用 POST 时出现相同的错误。

> xml <- xmlParse(file = "POST_bodyRequest.xml")
> r2 <- POST(url, autenticacao, body = list(xml))
Erro: All components of body must be named
> xml
<?xml version="1.0" encoding="UTF-8"?>
<rs:alarm-request xmlns:rs="http://www.ca.com/spectrum/restful/schema/request" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throttlesize="0" xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">
  <rs:requested-attribute id="0x10000"/>
  <rs:requested-attribute id="0x10001"/>
  <rs:requested-attribute id="0x10009"/>
  <rs:requested-attribute id="0x1000a"/>
  <rs:requested-attribute id="0x1006e"/>
  <rs:requested-attribute id="0x11ee8"/>
</rs:alarm-request>

> str(list(xml))
List of 1
 $ :Classes 'XMLInternalDocument', 'XMLAbstractDocument' <externalptr> 
> status_code(r2)
[1] 415

我对 R 中的 GET 请求没有任何问题。 POST 请求在 SoapUI 上运行良好。那么,我做错了什么?

【问题讨论】:

  • 你在做body = list(xml)。不应该是body = xml吗?
  • 我也试过了,结果一样。编辑:实际上,错误消息不同:“错误:body 的未知类型:必须为 NULL、FALSE、字符、原始或列表”
  • 您尝试使用upload_file 版本还是xmlParse 版本?似乎它应该在没有 list() 的情况下与前者一起使用

标签: r xml post


【解决方案1】:

嗯,它确实奏效了。问题与 upload_file() 函数或 xml 文件无关。这是 url 变量,未更新为 POST 版本。感谢您的确认,并检查此问题是否已解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多