systemdebt
systemdebt

Reputation: 4951

convert curl command to Rcurl

How do I convert this command:

curl -v -u abcdefghij1234567890:X -H "Content-Type: application/json" -X GET 'https://domain.freshdesk.com/api/v2/tickets'

to

curl command in Rcurl?

Upvotes: 3

Views: 3480

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78842

The dev version of curlconverter (devtools::install_github("hrbrmstr/curlconverter") can convert curl command-line strings with authentication and verbose params now:

Copy your URL to the clipboard:

curl -v -u abcdefghij1234567890:X -H "Content-Type: application/json" -X GET 'https://domain.freshdesk.com/api/v2/tickets'

Then run:

library(curlconverter)
req <- make_req(straighten())[[1]]

The following will now be in your clipboard:

httr::VERB(verb = "GET", url = "https://domain.freshdesk.com/api/v2/tickets", 
    httr::authenticate(user = "abcdefghij1234567890", 
        password = "X"), httr::verbose(), 
    httr::add_headers(), encode = "json")

but req is now also a callable function. You can see that by doing:

req
## function () 
## httr::VERB(verb = "GET", url = "https://domain.freshdesk.com/api/v2/tickets", 
##     httr::authenticate(user = "abcdefghij1234567890", password = "X"), 
##     httr::verbose(), httr::add_headers(), encode = "json")

or by actually calling it:

req()

I usually reformat the function source to make it more readable:

httr::VERB(verb = "GET", 
           url = "https://domain.freshdesk.com/api/v2/tickets", 
           httr::authenticate(user = "abcdefghij1234567890", password = "X"),
           httr::verbose(), 
           httr::add_headers(), 
           encode = "json")

and you can easily translate that to a plain GET call without namespacing:

GET(url = "https://domain.freshdesk.com/api/v2/tickets", 
    authenticate(user = "abcdefghij1234567890", password = "X"), 
    verbose(), 
    add_headers(), 
    encode = "json"))

We can validate it working with authenticated curl command-lines by a small substitution in your example:

curl_string <- 'curl -v -u abcdefghij1234567890:X -H "Content-Type: application/json" -X GET "https://httpbin.org/basic-auth/abcdefghij1234567890/X"'

make_req(straighten(curl_string))[[1]]()
## -> GET /basic-auth/abcdefghij1234567890/X HTTP/1.1
## -> Host: httpbin.org
## -> Authorization: Basic YWJjZGVmZ2hpajEyMzQ1Njc4OTA6WA==
## -> User-Agent: libcurl/7.43.0 r-curl/1.2 httr/1.2.1
## -> Accept-Encoding: gzip, deflate
## -> Accept: application/json, text/xml, application/xml, */*
## -> 
## <- HTTP/1.1 200 OK
## <- Server: nginx
## <- Date: Tue, 30 Aug 2016 14:13:12 GMT
## <- Content-Type: application/json
## <- Content-Length: 63
## <- Connection: keep-alive
## <- Access-Control-Allow-Origin: *
## <- Access-Control-Allow-Credentials: true
## <- 
## Response [https://httpbin.org/basic-auth/abcdefghij1234567890/X]
##   Date: 2016-08-30 14:13
##   Status: 200
##   Content-Type: application/json
##   Size: 63 B
## {
##   "authenticated": true, 
##   "user": "abcdefghij1234567890"
## }

Upvotes: 6

Rentrop
Rentrop

Reputation: 21507

You can use httr to do this as follows:

require(httr)
GET('https://domain.freshdesk.com/api/v2/tickets',
    verbose(),
    authenticate("user", "passwd"),
    content_type("application/json"))

Upvotes: 2

Related Questions