【问题标题】:Rook webserver parses content of postfields into name of listRook 网络服务器将 postfields 的内容解析为列表名称
【发布时间】:2016-03-03 15:31:25
【问题描述】:

我想将xml 字符串发送到我的Rook 网络服务器。但是当我使用Rook::Request 类的POST 方法解析我的请求的POST 有效负载时,它会将内容放入返回列表的名称中。对应的列表值为NA。我使用postFormRCurl 包的postfields 选项来创建我的请求。下面是一个更详细的示例:

将此放入文件webserver.R

library(Rook)

s <- Rhttpd$new()

#set up web server app
s$add(
  name="xml_example",
  app=function(env) {
    req <- Request$new(env)

    #parse POST request
    tmp=req$POST()

    #create response
    res <- Rook::Response$new()

    #use dput and capture.output to return text of tmp
    res$write(capture.output(dput(tmp)))
    res$finish()
  }
)

#start web server on port 9000
s$start(port=9000)
#we will start the web server via Rscript and NOT via RStudio
Sys.sleep(.Machine$integer.max)

以下可以通过 RStudio 执行(Windows 用户可能需要更改一些命令)

library(RCurl)

#start web server outside of RStudio! Do not forget to kill it later
system("Rscript webserver.R &")

#send POST request
postForm("http://127.0.0.1:9000/custom/xml_example",
         .opts=list(postfields="<request>test</request>",
                    httpheader=c("content-type"="application/xml")))

返回

#[1] "structure(list(`<request>test</request>` = NA),
#                    .Names = \"<request>test</request>\")"

如您所见,xml 字符串被放入列表名称中。不完全是我所期待的。除了提取列表名称以获取xml 之外,如何正确完成此操作?我需要在RookRCurl 中设置选项吗?

顺便说一句:

#do not forget to kill the webserver
system("ps aux|grep webserver.R")
#system("kill yourPIDhere")

【问题讨论】:

    标签: xml r post rcurl r-rook-package


    【解决方案1】:

    原来是Rook 中的一个解析错误/功能。以post请求为例

    postForm("http://127.0.0.1:9000/custom/xml_example",
             .opts=list(postfields="xml=<request>test</request>",
                        httpheader=c("content-type"="application/xml")))
    

    这给出了结果

    #[1] "structure(list(xml = \"<request>test</request>\"), .Names = \"xml\")"
    

    如您所见,Rook 解析器假定输入的某种key=value 结构。这对xmls 来说是有问题的,因为它们可以包含使用= 符号的命名空间定义(在定义xml 版本时也可能在其他情况下)。

    无论如何,我拒绝了Rook,因为远程机器只能通过一些黑客来访问它(请参阅http://jeffreyhorner.tumblr.com/post/33814488298/deploy-rook-apps-part-ii)。顺便说一句,这个黑客对我不起作用。我现在正在使用plumber 包 - 就像一个魅力! (https://github.com/trestletech/plumber)

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 2022-11-11
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多