Marcin
Marcin

Reputation: 8054

What would be the R equivalent for this curl request

I have a request in curl like this

curl -H "Content-Type:application/json"             \
     -X GET 127.0.0.1:8084/clusterpredict/byheaders \
     -v                                             \
     -b "text1"                                     \
     -A "text2"

How can I perform the same operation in RCurl or httr library in R?

Upvotes: 2

Views: 1532

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78832

In httr:

  • -A / --user-agent translates to user_agent()
  • -b / --cookie translates to set_cookies() but you'll need to read in the cookie file and set them in the call to it (RCurl has constructs to read stored cookie files). I'm making this assumption since you didn't use the "COOKIE1=1; COOKIE2=b" format after -b. You can set them individually in set_cookies() as well
  • -H / --header translates to add_headers() but there are special functions for setting content type (see below)
  • -v / --verbose translates to verbose()
  • -X / --request translates to the actual VERB functions (in this case GET())

Here's one way to read cookies into a file for use in set_cookie() (if you are, indeed, using a cookie jar):

ctmp <- read.table("cookies.txt", sep="\t", header=FALSE, stringsAsFactors=FALSE)[,6:7]
crumbs <- setNames(as.character(as.character(ctmp$V6)), ctmp$V7)

So, your example would translate to httr as:

GET("http://127.0.0.1:8084/clusterpredict/byheaders", 
    content_type_json(),
    user_agent("text2"),
    set_cookies(.cookies=crumbs),
    verbose())

If you have individual cookies vs a cookie jar:

GET("http://127.0.0.1:8084/clusterpredict/byheaders", 
    content_type_json(),
    user_agent("text2"),
    set_cookies(COOKIE1="value1", COOKIE2="value2),
    verbose())

NOTE that httr will persist cookies between calls to the same domain in the same R session, so no need to keep specifying that file or those explicit cookie values in subsequent calls.

You can assign the value of the output to a variable then retrieve the content from it:

response <- GET("http://127.0.0.1:8084/clusterpredict/byheaders", 
    content_type_json(),
    user_agent("text2"),
    set_cookies(COOKIE1="value1", COOKIE2="value2),
    verbose())

result <- content(response)
print(str(result))

Generally, one would use the jsonlite package or xml2 package (depending on the result type) to do the parsing vs rely on the built-in httr parsing since you can control the output better. In this case, it's JSON, so:

library(jsonlite)
result <- fromJSON(content(response, as="text"))
print(str(result))

Upvotes: 5

timelyportfolio
timelyportfolio

Reputation: 6579

Without a live address, this is hard to test, but here is something to get you started with httr.

library(httr)

#curl    -H "Content-Type:application/json"      -X GET  127.0.0.1:8084/clusterpredict/byheaders    -v -b "text1" -A "text2"
GET(
  "127.0.0.1:8084/clusterpredict/byheaders",
  add_headers(
    "Content-Type" = "application/json"
  ),
  set_cookies("text1"),
  user_agent("text2"),
  verbose() #-v
)

Upvotes: 1

Related Questions