french_fries
french_fries

Reputation: 1

Write a complicated curl request's R equivalent

I have a curl request:

curl -k --insecure -X POST https://somehttp -H 'Content-Type: application/x-www-form-urlencoded' -H 'cache-control: no-cache' -d 'grant_type=password&client_id=my_id&username=admin&password=admin&client_secret=k6897pyy-1h7l-11q0-lp10-s20sg4erlq44' | jq .access_token

How could i write that request in R. I've seen examples of simple ones, but not this complicated.

Upvotes: 2

Views: 99

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174278

Obviously I can't test this with the example you provided, but this should get you close. First ensure you populate these variables correctly:

url       <- "http://www.somehttp.com"
my_id     <- "my_id"
username  <- "admin"
password  <- "admin"
my_secret <- "k6897pyy-1h7l-11q0-lp10-s20sg4erlq4"

Now the following code prepares the request, sends it, and parses the response (I'm assuming it is json)

library(httr)

H <- add_headers(`Content-Type`= "application/x-www-form-urlencoded",
                  `cache-control` = "no-cache")

form_body <- list(grant_type    = "password", 
                  client_id     = my_id,
                  username      = username, 
                  password      = password,
                  client_secret = my_secret)

res <- POST(url = url, body = form_body, encode = "form", H)

content(res, "parsed")

Upvotes: 1

Related Questions