通过RCurl包转换卷曲code为R? [英] Convert curl code into R via the RCurl package?

查看:148
本文介绍了通过RCurl包转换卷曲code为R?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用将R和RCurl包写入下面的卷曲POST方法?

 卷曲-k -u名为myusername:输入mypassword -d{文:世界,你好!,水平:菜鸟}'-H内容类型:应用程序/ JSON-H接受:应用/ JSONhttp://api.website/v1/access?


解决方案

最后编辑:电子邮件伟大的邓肯朗寺之后,我有一个解决办法:

 库(RCurl)
库(RJSONIO)
postForm(http://api.website/v1/access?,
         .opts =列表(postfields =的toJSON(列表(文字=世界,你好!,水平=菜鸟)),
                      的HTTPHeader = C(Content-Type的=应用/ JSON,接受='应用/ JSON),
                      userpwd =用户名:密码
                      ssl.verifypeer = FALSE))

我的previous后低于但它现在所有被取代由上面(我已经离开了以下供参考)编辑:

我想知道这个问题的答案我自己。连看TARehman的回答我还是不知道如何做到这一点(我缺乏了解无疑是根目录)。

如果您下载从它的网站(我用的是普通的双赢二进制版本)卷曲,那么这里就是通过系统命令,它就像使用窗口的命令行窗口,但是从R在R做的一种方式:

 输出<  - 系统('C:/ + /卷曲7.27.0-RTMP-SSH2-SSL-SSPI-zlib的-IDN静态斌-W32 /卷曲 - ķ-u用户名:密码-d{\\\\文本\\\\:\\\\的Hello World \\\\,\\\\\\\\的水平!\\\\菜鸟\\\\}-H内容类型:应用程序/ JSON-H接受:应用/ JSON?http://api.website/v1/access',实习生= TRUE))

以上作品与API的使用我的一个。然后,您可以使用

我真的希望你找到答案,因为这将是有益的,我太知道了。

修改:使用带有RCurl实际API通过@ TARehman的例子我尝试:

下面是卷曲code的正常工作:

  X =系统('C:/ + /卷曲7.27.0-RTMP-SSH2-SSL-SSPI-zlib的-IDN静态斌-W32 /卷曲-k  - U用户名:密码-d{\\\\文本\\\\:\\\\有一个愉快的一天\\\\!}-H内容类型:应用程序/ JSON-H接受:应用/ JSON http://api.theysay.io:60000/v1/sentiment?',实习生= TRUE)

,这里是我是如何在RCurl改写它:

  curl.opts<  - 列表(userpwd =用户名:密码
                  的HTTPHeader =内容类型:应用程序/ JSON
                  的HTTPHeader =接受:应用/ JSON
                  超时= 20,
                  connecttimeout = 20,
                  详细= TRUE,
                  用户代理=RCurl
                  cainfo =执行。系统(CurlSSL,cacert.pem,包=RCurl))postForm(http://api.theysay.io:60000/v1/sentiment?,.params = C(数据='{\\\\文本\\\\:\\\\有一个愉快的一天\\\\!} '),.opts = curl.opts)

不过,这并没有工作,结果如下(显然我没有理解正确的东西):

  *关于为()连接到api.theysay.io端口60000(#0)
*尝试163.1.94.100 ... *连接
*连接到api.theysay.io(163.1.94.100)端口60000(#0)
> POST / V1 /情绪? HTTP / 1.1
用户代理:RCurl
主持人:api.theysay.io:60000
接受:应用/ JSON
内容长度:193
期望:100-继续
内容类型:多重/ form-data的;边界= ---------------------------- 7620b19c7a5d< HTTP / 1.1 100继续
< HTTP / 1.1 401未经授权
< WWW身份验证:基本境界=保护资源
<内容类型:纯文本/
<服务器:喷雾罐/ 1.0-M2.1
<日期:星期三,2012年9月12日12时26分06秒GMT
<内容长度:77
<
*忽略响应体
*连接#0主办api.theysay.io保持不变
*发出另一个请求这个网址:'?http://api.theysay.io:60000/v1/sentiment
*重新使用现有的连接! (#0)与主机api.theysay.io
*连接到api.theysay.io(163.1.94.100)端口60000(#0)
使用基本与用户'[USERNAME]'*服务器授权
> POST / V1 /情绪? HTTP / 1.1
授权:基本[KEY] ==
用户代理:RCurl
主持人:api.theysay.io:60000
接受:应用/ JSON
内容长度:193
期望:100-继续
内容类型:多重/ form-data的;边界= ---------------------------- 949d191bdaeb< HTTP / 1.1 100继续
< HTTP / 1.1 415不支持的媒体类型
<内容类型:纯文本/
<服务器:喷雾罐/ 1.0-M2.1
<日期:星期三,2012年9月12日12时26分06秒GMT
<内容长度:93
<
*连接#0主办api.theysay.io保持不变
[1]有与请求的Content-Type一个问题:\\ n期望'文本/ XML或应用/ JSON'
ATTR(内容类型)text / plain的

How I would write the following curl POST method using R and the RCurl package?

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?"

解决方案

FINAL EDIT: After emailing the great Duncan Temple Lang I have a solution:

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))

My previous post is below but it has all now been superseded by the edit above (I've left the below for reference):

I would like to know the answer to this question myself. even looking at TARehman's answer I still can't work out how to do it (my lack of understanding is no doubt the root).

If you download curl from its website (I used the generic-win binary version), then here's one way of doing it in R via the 'system' command which is like using the window's command line window but from 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))

The above works with one of the API's I use. You can then use

I really hope you find an answer because it would be useful for me to know too.

EDIT: My attempt using an actual API with RCurl via @TARehman's example:

Here is the curl code which works fine:

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)

And here is how I've rewritten it in 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)

This however did not work and results in the following (clearly I have not understood something correctly):

* 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" 

这篇关于通过RCurl包转换卷曲code为R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆