【问题标题】:Rcurl with http data post带有 http 数据帖子的 Rcurl
【发布时间】:2013-08-28 16:47:16
【问题描述】:

我想将以下 curl 调用移至 Rcurl:

curl  'http://myserver.org/stream' 
-H 'Authorization: Basic XXXXXXXX' -H 'Connection: keep-alive' --data-binary '{"limit": 20}' 
-H 'Content-Type: application/json;charset=UTF-8'

这是我的 R 测试之一:

library(RCurl)
url.opts <- list(httpheader = list(Authorization ="Basic XXXXXXXX", 
                Connection = "keep-alive", 
                "Content-Type" = "application/json;charset=UTF-8"))

getURLContent('http://myserver.org/stream', .opts=url.opts)

现在我缺少添加--data 或--data-binary 的属性。如何添加这个选项?

非常感谢 马库斯

【问题讨论】:

  • 你可以使用getURLContent(..., customrequest = 'POST', postfields = '{"limit": 20}')或类似的东西。
  • 使用httr 可能会更容易一些:POST('http://myserver.org/stream', c(authenticate(u, p, "basic"), accept_json(), body = '{"limit": 20}')

标签: r rcurl


【解决方案1】:

非常感谢@hadley。

这是我的工作解决方案:

library(httr)
user <- "test"
password <- "test123"
POST(url = 'http://myserver.org/stream', 
     config = c(authenticate(user, password, "basic"), 
                add_headers(Connection = "keep-alive"), 
                accept_json()), 
     body = "{'username':'test'}")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多