8

我将如何使用 R 和 RCurl 包编写以下 curl POST 方法?

curl -k -u myusername:mypassword -d '{"text":"Hello World!","level":"Noob"}' -H "Content-Type: application/json" -H "Accept: application/json" "http://api.website/v1/access?"
4

2 回答 2

13

最终编辑:在给伟大的 Duncan Temple Lang 发送电子邮件后,我有一个解决方案:

library(RCurl)
library(RJSONIO)
postForm("http://api.website/v1/access?",
         .opts = list(postfields = toJSON(list(text = "Hello World!", level = "Noob")),
                      httpheader = c('Content-Type' = 'application/json', Accept = 'application/json'),
                      userpwd = "Username:Password",
                      ssl.verifypeer = FALSE))

我以前的帖子在下面,但现在它都被上面的编辑所取代(我把下面的内容留作参考):

我想自己知道这个问题的答案。即使看着 TARehman 的回答,我仍然无法弄清楚该怎么做(我缺乏理解无疑是根源)。

如果您从其网站下载 curl(我使用的是 generic-win 二进制版本),那么这是通过“系统”命令在 R 中执行此操作的一种方法,就像使用窗口的命令行窗口但来自 R:

output <- system('C:/+/curl-7.27.0-rtmp-ssh2-ssl-sspi-zlib-idn-static-bin-w32/curl  -k -u "username:password" -d "{\\"text\\":\\"Hello World!\\",\\"level\\":\\"Noob\\"}" -H "Content-Type: application/json" -H "Accept: application/json" "http://api.website/v1/access?"', intern = TRUE))

以上适用于我使用的 API 之一。然后你可以使用

我真的希望你能找到答案,因为这对我来说也很有用。

编辑:我尝试通过@TARehman 的示例使用带有 RCurl 的实际 API:

这是可以正常工作的卷曲代码:

x = system('C:/+/curl-7.27.0-rtmp-ssh2-ssl-sspi-zlib-idn-static-bin-w32/curl  -k -u "USERNAME:PASSWORD" -d "{\\"text\\":\\"Have a nice day!\\"}" -H "Content-Type: application/json" -H "Accept: application/json" "http://api.theysay.io:60000/v1/sentiment?"', intern = TRUE)

这是我在 RCurl 中重写它的方式:

curl.opts <- list(userpwd = "username:password", 
                  httpheader = "Content-Type: application/json",
                  httpheader = "Accept: application/json",
                  timeout = 20, 
                  connecttimeout = 20, 
                  verbose = TRUE, 
                  useragent = "RCurl",
                  cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))

postForm("http://api.theysay.io:60000/v1/sentiment?", .params = c(data = '{\\"text\\":\\"Have a nice day!\\"}'), .opts = curl.opts)

但是,这不起作用并导致以下结果(显然我没有正确理解某些内容):

* About to connect() to api.theysay.io port 60000 (#0)
*   Trying 163.1.94.100... * connected
* Connected to api.theysay.io (163.1.94.100) port 60000 (#0)
> POST /v1/sentiment? HTTP/1.1
User-Agent: RCurl
Host: api.theysay.io:60000
Accept: application/json
Content-Length: 193
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------7620b19c7a5d

< HTTP/1.1 100 Continue
< HTTP/1.1 401 Unauthorized
< WWW-Authenticate: Basic realm="Secured Resource"
< Content-Type: text/plain
< Server: spray-can/1.0-M2.1
< Date: Wed, 12 Sep 2012 12:26:06 GMT
< Content-Length: 77
< 
* Ignoring the response-body
* Connection #0 to host api.theysay.io left intact
* Issue another request to this URL: 'http://api.theysay.io:60000/v1/sentiment?'
* Re-using existing connection! (#0) with host api.theysay.io
* Connected to api.theysay.io (163.1.94.100) port 60000 (#0)
* Server auth using Basic with user '[USERNAME]'
> POST /v1/sentiment? HTTP/1.1
Authorization: Basic [KEY]==
User-Agent: RCurl
Host: api.theysay.io:60000
Accept: application/json
Content-Length: 193
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------949d191bdaeb

< HTTP/1.1 100 Continue
< HTTP/1.1 415 Unsupported Media Type
< Content-Type: text/plain
< Server: spray-can/1.0-M2.1
< Date: Wed, 12 Sep 2012 12:26:06 GMT
< Content-Length: 93
< 
* Connection #0 to host api.theysay.io left intact
[1] "There was a problem with the requests Content-Type:\nExpected 'text/xml' or 'application/json'"
attr(,"Content-Type")

"text/plain" 
于 2012-09-10T08:48:15.390 回答
3

在 RCurl 中,有一个名为 postForm() 的函数,这是您想要使用的。我的通用示例:

library(RCurl)

param1 <- "json"
param2 <- "all"
param3 <- "j3kn1kn41" # Hypothetical API token

webobj <- postForm("http://www.thewebsite.com/api/",type=param1,records=param2,token=param3)

基本上,您只需将所需的任何参数作为命名参数连同基本 URL 一起传递给 postForm(),然后它构造并发布它,并将响应放入您声明的任何对象 (webobj) 在这种情况下。

如果您需要设置任何 curl 选项,您可以使用.opts=curlOptions(OPTIONS HERE)来更改它们。

于 2012-09-06T15:10:39.533 回答