Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass user and password in new_handle in curl R

Tags:

curl

r

I have curl request that is working file, which is as follows:

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

How I want to do it using curl package in R

I have following code,

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

Now I don't know how to pass username and password in this curl request

like image 381
Kush Patel Avatar asked Aug 16 '17 21:08

Kush Patel


People also ask

How do I pass username and password in curl request?

For example, if a website has protected content curl allows you to pass authentication credentials. To do so use the following syntax: curl --user "USERNAME:PASSWORD" https://www.domain.com . “USERNAME” must be replaced with your actual username in quotes.

What is curl package in R?

The curl package provides bindings to the libcurl C library for R. The package supports retrieving data in-memory, downloading to disk, or streaming using the R “connection” interface. Some knowledge of curl is recommended to use this package.


1 Answers

You should set httpauth and userpwd options too:

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"
like image 156
Artem Klevtsov Avatar answered Oct 09 '22 21:10

Artem Klevtsov