简体   繁体   English

R 中的 POST 请求:upload_file() 中的错误 xml

[英]POST request in R: error in upload_file() with xml

I'm trying to create a POST request, but the body parameter isn't working as I expected.我正在尝试创建一个 POST 请求,但body参数没有按预期工作。

The POST_bodyRequest.xml file 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>

The code, basically the POST call代码,基本上是 POST 调用

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

First thing to note is that the content from the file isn't saved in "xml" file:首先要注意的是文件中的内容没有保存在“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"

Therefore, the POST call returns an error因此,POST 调用返回错误

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

I've also tried do read the xml file using xmlParse() .我也尝试过使用xmlParse()读取 xml 文件。 In this case, the code is recovered as expected, but I get the same error when calling POST.在这种情况下,代码按预期恢复,但在调用 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

I had no trouble with GET requests in R.我对 R 中的 GET 请求没有任何问题。 The POST request works fine with SoapUI. POST 请求适用于 SoapUI。 So, what am I doing wrong?那么,我做错了什么?

Well, it did work.嗯,它确实奏效了。 The problem wasn't anything related to the upload_file() function or the xml file.问题与 upload_file() function 或 xml 文件无关。 It was the url variable, which wasn't updated to the POST version.它是 url 变量,未更新到 POST 版本。 I'd like to thank for the confirmations and check this issue as resolved.我要感谢您的确认并检查此问题是否已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM