Rook 网络服务器将 postfields 的内容解析为列表名称
Rook webserver parses content of postfields into name of list
我想向我的 Rook
网络服务器发送一个 xml
字符串。但是当我使用 Rook::Request
class 的 POST
方法来解析我请求的 POST
负载时,它将内容放入返回列表的名称中。对应的列表值为NA
。我使用 postForm
和 RCurl
包的 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")))
这个returns
#[1] "structure(list(`<request>test</request>` = NA),
# .Names = \"<request>test</request>\")"
如您所见,xml
字符串被放入列表名称中。不完全是我所期待的。除了提取列表名称以获取 xml
之外,如何正确完成此操作?我需要在 Rook
或 RCurl
中设置选项吗?
顺便说一句:
#do not forget to kill the webserver
system("ps aux|grep webserver.R")
#system("kill yourPIDhere")
原来是Rook
内的解析error/feature。以 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
结构。这对 xml
来说是有问题的,因为它们可以包含使用 =
符号的名称空间定义(在定义 xml
版本时,可能在其他情况下也是如此)。
无论如何,我拒绝了 Rook
,因为远程机器只能通过一些 hack 访问它(参见 http://jeffreyhorner.tumblr.com/post/33814488298/deploy-rook-apps-part-ii). This hack, BTW, does not work for me. I am using the plumber
package now - works like a charm! (https://github.com/trestletech/plumber)
我想向我的 Rook
网络服务器发送一个 xml
字符串。但是当我使用 Rook::Request
class 的 POST
方法来解析我请求的 POST
负载时,它将内容放入返回列表的名称中。对应的列表值为NA
。我使用 postForm
和 RCurl
包的 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")))
这个returns
#[1] "structure(list(`<request>test</request>` = NA),
# .Names = \"<request>test</request>\")"
如您所见,xml
字符串被放入列表名称中。不完全是我所期待的。除了提取列表名称以获取 xml
之外,如何正确完成此操作?我需要在 Rook
或 RCurl
中设置选项吗?
顺便说一句:
#do not forget to kill the webserver
system("ps aux|grep webserver.R")
#system("kill yourPIDhere")
原来是Rook
内的解析error/feature。以 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
结构。这对 xml
来说是有问题的,因为它们可以包含使用 =
符号的名称空间定义(在定义 xml
版本时,可能在其他情况下也是如此)。
无论如何,我拒绝了 Rook
,因为远程机器只能通过一些 hack 访问它(参见 http://jeffreyhorner.tumblr.com/post/33814488298/deploy-rook-apps-part-ii). This hack, BTW, does not work for me. I am using the plumber
package now - works like a charm! (https://github.com/trestletech/plumber)