简单的卷曲-H在R中

时间:2015-06-23 21:54:15

标签: r curl rcurl

我想做

curl -H "Authorization: Basic YOUR_API_KEY" -d '{"classifier_id":155, "value":"TEST"}' "https://www.machinelearningsite.com/language/classify"

我试过

  h = getCurlHandle(header = TRUE, userpwd = YOUR_API_KEY, netrc = TRUE)
out <- getURL("https://www.machinelearningsite.com/language/classify?classifier_id=155&value=TEST", curl=h,ssl.verifypeer=FALSE)

但它说方法不允许

1 个答案:

答案 0 :(得分:5)

curl命令行参数转换为httr次调用要容易得多:

library(httr)

result <- GET("https://www.machinelearningsite.com/language/classify",
              add_headers(Authorization=sprintf("Basic %s", YOUR_API_KEY),
              query=list(classifier_id=155, value="TEST")))

理想情况下,YOUR_API_KEY将是一个环境变量,因此您可以将其更改为:

result <- GET("https://www.machinelearningsite.com/language/classify",
              add_headers(Authorization=sprintf("Basic %s", Sys.getenv("YOUR_API_KEY")),
              query=list(classifier_id=155, value="TEST")))              

然后你可以这样做:

content(result)

检索实际数据。