-1

I want to do

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

I tried

  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)

but it says method not allowed

user670186
  • 2,588
  • 6
  • 37
  • 55

1 Answers1

6

It's much easier to translate curl command-line arguments into httr calls:

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

ideally, YOUR_API_KEY would be an environment variable, so you can change that to:

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

You can then do:

content(result)

To retrieve the actual data.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Error in add_headers(Authorization = sprintf("Basic %s", key), query = params(classifier_id = model, : could not find function "params" – user670186 Jun 24 '15 at 09:37
  • I tried out <- GET("https://www.classifier.io/language/classify", add_headers(Authorization=sprintf("Basic %s", "abcdefg"), query=list(classifier_id="1234", value="TESTtetx"))) – user670186 Jun 24 '15 at 09:58
  • Error: is.character(headers) is not TRUE – user670186 Jun 24 '15 at 10:00
  • aye. it should have been `list`. fixed it. I can't help more without a valid site to test against. you can even look here http://stackoverflow.com/a/22672642/1457051 for another sample with `httr` and `add_headers` – hrbrmstr Jun 24 '15 at 10:15
  • 1
    I think you can use `authenticate()` instead of manually adding headers like a savage – hadley Jun 24 '15 at 19:06
  • That validation is good though. I thought was much when looking at the command line incantation. Sites really should standardize. – hrbrmstr Jun 25 '15 at 01:45