2011-04-26 278 views
13

作为探索如何在R for Denver RUG中创建一个包的一种方法,我决定在datasciencetoolkit API上编写一个R包装器将是一个有趣的小项目。基本的R工具来自RCurl包,正如你可能想象的那样。我被困在一个看似简单的问题上,我希望这个论坛上的某个人能够指引我朝着正确的方向发展。基本的问题是,我似乎无法使用postForm()传递一个未键控的字符串作为curl中数据选项的一部分,即curl -d“string”“address_to_api”。使用RCurl的POST请求

例如,在命令行中我可能会做

$ curl -d "Tim O'Reilly, Archbishop Huxley" "http://www.datasciencetoolkit.org/text2people" 

成功。但是,postForm()在将其他参数传递到POST请求时需要显式键。我已经通过datasciencetoolkit代码和开发人员文档查找了可能的密钥,但似乎无法找到任何内容。

顺便说一句,通过GET请求将输入传递给DSTK API的其他部分非常简单。例如,

ip2coordinates <- function(ip) { 
    api <- "http://www.datasciencetoolkit.org/ip2coordinates/" 
    result <- getURL(paste(api, URLencode(ip), sep="")) 
    names(result) <- "ip" 
    return(result) 
} 
ip2coordinates('67.169.73.113') 

会产生期望的结果。

为了清楚起见,我已阅读了DTL的omegahat网站上的RCurl文档,包中的RCurl文档以及卷曲手册页。然而,我错过了关于curl(或postForm()函数中的.opts())的基本概念,我似乎无法得到它。

在python中,我基本上可以使用httplib.HTTPConnection创建'原始'POST请求 - 类似于R中可用的那样?我也查看了httpRequest包中的simplePostToHost函数,它似乎锁定了我的R会话(它似乎也需要一个密钥)。

FWIW,我在Mac 10.6.7上使用R 2.13.0。

任何帮助,非常感谢。如果您有兴趣使用数据科学工具包,所有代码即将在github上提供。

干杯。

回答

15

随着HTTR,这仅仅是:

library(httr) 
r <- POST("http://www.datasciencetoolkit.org/text2people", 
    body = "Tim O'Reilly, Archbishop Huxley") 
stop_for_status(r) 
content(r, "parsed", "application/json") 
6

通常,在您尝试发布未键控的某些内容的情况下,您可以为该值分配一个虚拟键。例如:

> postForm("http://www.datasciencetoolkit.org/text2people", a="Archbishop Huxley") 
[1] "[{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":44,\"end_index\":61,\"matched_string\":\"Archbishop Huxley\"},{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":88,\"end_index\":105,\"matched_string\":\"Archbishop Huxley\"}]" 
attr(,"Content-Type") 
       charset 
"text/html"  "utf-8" 

将工作一样,如果我想用B =“大主教赫胥黎”等

享受RCurl - 它可能是我最喜欢的[R包。如果你喜欢冒险,升级到〜libcurl 7.21会通过curl(包括SMTP等)公开一些新的方法。

+0

感谢您的帮助!有没有任何理由,关键是'一个'?我尝试了'名称','文本'和一堆其他垃圾。 – rtelmore 2011-04-27 02:24:22

+0

更正:我尝试在不同的调用中使用“名称”等。我尝试过使用,例如postForm(api,string),你需要postForm(api,a = string)。 – rtelmore 2011-04-27 04:43:56

+0

对,你需要提供一个key = value对。 'a'完全是任意的(这只是想到的第一个字母)。其他任何工作都一样(例如a =“string”,name =“string”等。“a”=“string”将不起作用。) – Noah 2011-04-27 09:19:19

1

我只想指出,通过postForm函数传递一个原始字符串时一定会有问题。例如,如果我用卷曲在命令行中,我得到如下:

$ curl -d "Archbishop Huxley" "http://www.datasciencetoolkit.org/text2people 
[{"gender":"u","first_name":"","title":"archbishop","surnames":"Huxley","start_index":0,"end_index":17,"matched_string":"Archbishop Huxley"}] 

和RI获得

> api <- "http://www.datasciencetoolkit.org/text2people" 
> postForm(api, a="Archbishop Huxley") 
[1] "[{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":44,\"end_index\":61,\"matched_string\":\"Archbishop Huxley\"},{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":88,\"end_index\":105,\"matched_string\":\"Archbishop Huxley\"}]" 
attr(,"Content-Type") 
       charset 
"text/html"  "utf-8" 

注意,它返回的JSON字符串两个元素,并没有一个匹配上start_index或end_index。这是编码或其他问题吗?

+0

我猜这实际上是API端的东西 - 这是你期望看到的类型,如果他们奇怪地处理URL编码的东西。你可以尝试在你的论点上使用URLencode(),但这可能实际上没有帮助。 – Noah 2011-04-29 21:10:42

1

的simplePostToHost功能放在HttpRequest包可能你在找什么在这里。

2

从邓肯寺郎的R-帮助列表上:

postForm()是使用比提交卷曲-d命令形式的不同的式(或特异性的Content-Type)。 切换style ='POST'使用相同的类型,但很快猜测,参数名称'a'导致混淆 ,结果是空的JSON数组 - “[]”。

一个快速的解决方法是使用curlPerform(),而不是直接postForm()

r = dynCurlReader() 
curlPerform(postfields = 'Archbishop Huxley', url = 'http://www.datasciencetoolkit.org/text2people', verbose = TRUE, 
      post = 1L, writefunction = r$update) 
r$value() 

这就产生

[1] 
"[{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":0,\"end_index\":17,\"matched_string\":\"Archbishop 
Huxley\"}]" 

,您可以使用fromJSON()将其改造成R.数据