2017-08-16 90 views
1

我有一個工作文件捲曲的要求,主要內容如下:如何new_handle通過用戶名和密碼在捲曲[R

curl -XGET "https://xxxx.com/xxx" -u "username:password"

我想怎麼用curl包中的R

它做的事

我有下面的代碼,

library(curl) clCall <- new_handle(url = 'https://xxxx.com/xxx') handle_setopt(clCall, customrequest = "XGET")

現在我不知道如何通過用戶名和密碼在這種捲曲請求

+0

你嘗試過這個 clCall < - new_handle(URL = 'https://xxxx.com/xxx', userpwd ='user:password') – eclark

+0

沒有工作,我試過userpwd,而用戶 –

+0

它拋出了什麼錯誤?你有沒有嘗試使用'RCurl'包來代替'curl'? – eclark

回答

1

您應該設置httpauthuserpwd選項太:

h <- curl::new_handle() 
curl::handle_setopt(
    handle = h, 
    httpauth = 1, 
    userpwd = "user:passwd" 
) 
resp <- curl::curl_fetch_memory("https://httpbin.org/basic-auth/user/passwd", handle = h) 
jsonlite::fromJSON(rawToChar(resp$content)) 
#> $authenticated 
#> [1] TRUE 
#> 
#> $user 
#> [1] "user"