R中的卷曲请求

时间:2017-09-06 10:51:06

标签: r curl

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"}

我需要在R中执行相同的操作:

这是我到目前为止所尝试的:

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)

作为回应,

print(h $ value()[&#39; Set-Cookie&#39;])

我可以访问Set-Cookie。但是如何访问viz.sess3的价值?

1 个答案:

答案 0 :(得分:1)

使用curl包的示例:

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))