Nick
Nick

Reputation: 286

Multiple Authentication in CURL

Context

I want to access an API from R and have the correct credentials to do so.

The issue is that Authentication requires three arguments to be passed (Username, Password and Organisation).

I am not familiar with API calls from R and I do not know how to pass this "organisation" argument.

Example

An example of the API call to request a token is as follows (taken from the documentation):

curl -X POST "https://URL.com" \
-H "Content-Type: application/json" \
--data "{\"user_name\": \"<username>\" \
\"password\": \"<mypassword>\",\
\"organisation\": \"<organisation>\"}"

Using the crul package, I have tried:

require("crul")

z <- crul::HttpClient$new(url = "https://THEURL.com", 
    auth(user = "myuser", 
         pwd = "mypwd"))
z$get()

Which returns:

<crul response>
url: https://THEURL.com
request_headers: 
User-Agent: libcurl/7.59.0 r-curl/3.3 crul/0.8.0
Accept-Encoding: gzip, deflate
Accept: application/json, text/xml, application/xml, */*
response_headers: 
status: HTTP/1.1 403 Forbidden
content-type: application/json
content-length: 211
connection: keep-alive
x-amzn-errortype: IncompleteSignatureException

Im assuming that x-amzn-errortype is referring to the missing organisation authentication variable.

Questions

How can I pass the variable "Organisation" to the function?

Or is there a better way to do this?

Upvotes: 0

Views: 368

Answers (1)

AEF
AEF

Reputation: 5670

I think it will be way easier for you to use the package httr when you deal with HTTP requests. I think your example request could be:

library(httr)

POST("https://THEURL.com",
     body = list(user_name = "<username>", password = "<mypassword>", organisation = "<organisation>"),
     encode = "json")

Upvotes: 1

Related Questions