我正在尝试创建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上正常工作。所以,我在做什么错?
嗯,它确实起作用了。问题不在于与upload_file()函数或xml文件有关。这是url变量,没有更新为POST版本。非常感谢您的确认,并确认此问题已解决。