systemdebt
systemdebt

Reputation: 4951

curl request in R

curl req:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -d '{"username":"emailId","password":"passwrd"}' -X POST https://central.vizury.com/-/api/login

res:

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Content-Type: application/json; charset=utf-8
Date: Wed, 06 Sep 2017 10:47:00 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: viz.sess3=SessionCookieHere; path=/; expires=Wed, 06 Sep 2017 10:49:01 GMT; secure; httponly
Set-Cookie: AWSELB=someval;PATH=/;EXPIRES=Wed, 06 Sep 2017 10:49:01 GMT;SECURE;HTTPONLY
Vary: Accept-Encoding
X-Powered-By: Express
Content-Length: 226
Connection: keep-alive

{"status":"OK","results":{"username":"email","role":"role","products":["webConvert","mobiConvert"],"needsNewPassword":false},"homePath":"/webConvert/#/dashboard/campaignName"}

I need to perform the same action in R:

This is what I have tried so far:

h <- basicHeaderGatherer()

loginUrl <- "https://central.vizury.com/-/api/login"
params <- list('username' = 'username',
               'password' = 'password')
loginRes <- postForm(loginUrl, .params=params, style="POST",  .opts=curlOptions(headerfunction=h$update, verbose=TRUE))
print("loginres")
print(loginRes)

In response,

print(h$value()['Set-Cookie'] )

I can access Set-Cookie. But how do I access the value of viz.sess3?

Upvotes: 1

Views: 399

Answers (1)

Artem Klevtsov
Artem Klevtsov

Reputation: 9433

Example using curl package:

h <- curl::new_handle()
login_url <- 'https://central.vizury.com/-/api/login'
curl::handle_setform(
    handle = h,
    username = 'username',
    password = 'password'
)
resp <- curl::curl_fetch_memory(login_url, handle = h)
message(resp$status_code)
jsonlite::fromJSON(rawToChar(resp$content))

Upvotes: 1

Related Questions