Simple Curl -H in R

1.2k views Asked by At

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

1

There are 1 answers

6
hrbrmstr On

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.