Basic authentication with the crul R package

,

Hi everyone,

Thanks for the really nice crul package. I would like to use it to create a R api package but I have an issue.
I tried to dig into the documentation with no success.

I want to know how to do basic http (or other methods) authentication like in Python requests or with R package httr.

Here are two examples of what I want to achieve with crul (let me know if it is right place such question or I should as it at SO).

import requests
from requests.auth import HTTPBasicAuth
requests.get("https://api.github.com/user", auth = ('username', 'password'))

Or with the package R httr

library(httr)
GET(url = 'https://api.github.com/user', authenticate(user = 'username', password = 'password'))

Thanks again for the support

Thanks for your question @dickoa

We didn’t have anything to support that yet - though you could do it with curl options yourself, but that’s a bit tedious if you aren’t familiar with them.

Reinstall devtools::install_github("ropensci/crul") and see ?auth

e.g.

library(crul)
res <- HttpClient$new(
  url = "https://httpbin.org/basic-auth/user/passwd",
  auth = auth(user = "user", pwd = "passwd")
)
res$auth
x <- res$get()
jsonlite::fromJSON(x$parse("UTF-8"))
1 Like

Thanks a lot @sckott, it works perfectly.
Thanks again for this package it’s really a nice addition to the web stack in R.

Best,

glad it works :smiley: