Curl dropbox request in R using httr - r

I have this sample curl request:
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer fakeaccesstoke12345" \
--header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\",\"mode\": \"add\"}" \
--header "Content-Type: application/octet-stream" \
--data-binary #local_file.txt
Which I tried translating into httr like this:
httr::POST(
"https://content.dropboxapi.com/2/files/upload",
add_headers(Authorization = "Bearer fakeaccesstoke12345",
`Dropbox-API-Arg` = paste("{\"path\": \"/folder/", FileName, "\"mode\": \"add\"}", sep = ''),
`Content-Type` = "Content-Type: application/octet-stream"
)
)
This is not working. I'm not sure what the --data-binary is doing either. The docs don't seem to say anything about it so I wonder if this is some standard parameter in HTTP.
The docs for the upload enpoint can be found here if needed.

library(httr)
library(jsonlite)
POST(
'https://content.dropboxapi.com/2/files/upload',
add_headers(
Authorization = "Bearer <token>",
`Dropbox-API-Arg` =
jsonlite::toJSON(
list(path = "/books.csv", mode = "add", autorename = TRUE, mute = FALSE),
auto_unbox = TRUE
)
),
content_type("application/octet-stream"),
body = upload_file("books.csv")
)

Related

Send email with MailerSend API and R

I am trying to use the transaction email service MailerSend to send an email from R.
They have cURL instructions as to how to do this.
I don't think I have the formatting quite right as I get the error:
> response
Response [https://api.mailersend.com/v1/email]
Date: 2021-02-26 19:49
Status: 401
Content-Type: application/json
Size: 30 B
> rsp_content <- content(response, as = "parsed", type = "application/json")
> rsp_content
$message
[1] "Unauthenticated."
R Script to send an email
library("httr")
# MailerSend API Key
apiKeyMS <- Sys.getenv("MS_KEY")
# Email header info
email_from = "noreply#example.ca"
email_from_name = "Joe"
email_subject <- "Joe Update"
email_to <- "joe#gmail.com"
# Email Body
email_text <- "This is my email"
email_html <- "This is my email"
# Send Email
ms_url <- paste0("https://api.mailersend.com/v1/email")
response <- POST(ms_url,
add_headers("Authorization: Bearer" = apiKeyMS,
"X-Requested-With" = "XMLHttpRequest",
"Content-Type" = "application/json"),
body = list(
"subject" = email_subject,
"from" = email_from,
"from_name" = email_from_name,
"to" = email_to,
"text" = email_text,
"html" = email_html
), encode = "json")
#############################################################
Basic MailerSend cURL instructions
curl -X POST \
https://api.mailersend.com/v1/email \
-H 'Content-Type: application/json' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Authorization: Bearer {place your token here without brackets}' \
-d '{
"from": {
"email": "your#email.com"
},
"to": [
{
"email": "your#email.com"
}
],
"subject": "Hello from MailerSend!",
"text": "Greetings from the team, you got this message through MailerSend.",
"html": "Greetings from the team, you got this message through MailerSend."
}'
I don't know R at all, but I would guess your bearer token is incorrect.
The header key is Authorization and the value should be "Bearer {place your token here without brackets}"
You have to have these three headers
-H 'Content-Type: application/json' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Authorization: Bearer {place your token here without brackets}' \`

Reading API - code from Curl to R

I am trying to read a json from an authenticated API using R, but not sucessfully.
I have the Curl code and tried to convert it to R using "curlconverter" library and also tried to get it using "httr" library.
curl -X GET \
'https://api.cartolafc.globo.com/auth/liga/gurudocartola-com?orderBy=campeonato&page=1' \
-H 'Cache-Control: no-cache' \
-H 'x-glb-token: mytoken'
I would appreciate a solution to write this code in R.
library(curlconverter) # devtools::install_github("hrbrmstr/curlconverter")
u <- "curl -X GET 'https://api.cartolafc.globo.com/auth/liga/gurudocartola-com?orderBy=campeonato&page=1' -H 'Cache-Control: no-cache' -H 'x-glb-token: mytoken'"
straighten(u) %>%
make_req()
That makes:
httr::VERB(verb = "GET", url = "https://api.cartolafc.globo.com/auth/liga/gurudocartola-com?orderBy=campeonato&page=1",
httr::add_headers(`Cache-Control` = "no-cache",
`x-glb-token` = "mytoken"))
which very straightforwardly (if one has done any research before posting a question) translates to:
httr::GET(
url = "https://api.cartolafc.globo.com/auth/liga/gurudocartola-com",
httr::add_headers(
`Cache-Control` = "no-cache",
`x-glb-token` = "mytoken"
),
query = list(
`orderBy` = "campeonato",
`page` = 1L
)
)
The back-ticks are there solely to remind me they are parameters (and, they sometimes contain dashes or other chars which force a back-tick quote).

Translate Curl to R programming POST function

I am trying to translate this code into R programming:
curl -v -X POST -H "X-IBM-Client-Id:YOUR_CLIENT_ID" -H "X-IBM-Client-Secret:YOUR_CLIENT_SECRET" -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -H "Content-Type:application/json" -H "Accept:application/json" https://api.ibm.com/watsonanalytics/run/data/v1/datasets -d '{ "description" :"1234" , "name" :"1234" }'
But when i tried, it gives me an Unauthorized error.
Here it is the code I am using:
library(RCurl)
library(RJSONIO)
httpPOST("https://api.ibm.com/watsonanalytics/run/data/v1/datasets",
content= c("description"="Test1",
"name"="Test1"),
.opts = list(httpheader = c('Content-Type' = 'application/json',
'Accept' = 'application/json',
'X-IBM-Client-Secret' = '123',
'X-IBM-Client-Id' = '123',
'Authorization: Bearer'="")))
I really appreciate your help
Thank you

Using R complete -X POST in command line tool curl

I use the command line tool curl to post data and get response from server, the command is like this:
curl -X POST -H 'Content-Type: application/gpx+xml' -H 'Accept: application/json' --data-binary #gpslog.gpx "http://test.roadmatching.com/rest/mapmatch/?app_id=MY_APPID&app_key=MY_APPKEY" -o output.json
I tried using RCurl package to do the same thing, but it doesn't work. Can someone point me to the right direction? Thanks.
postForm(uri = "http://test.roadmatching.com/rest/mapmatch/?app_id=MYID&app_key=MYKEY",
.ops = list(httpheader = c('Content-type': 'application/gpx','Accept': 'application/json')),
.params = "/Users/data.gpx")
With httr - not tested, you may have to tweak it a bit
url <- "http://test.roadmatching.com/rest/mapmatch"
args <- list(app_id = "MY_APPID", app_key = "MY_APPKEY")
gpxxml <- add_headers(`Content-Type` = "application/gpx+xml")
httr::POST(url, query = args, gpxxml, accept_json(),
write_disk("output.json"), body = upload_file("gpslog.gpx"))

Rcurl with http data post

I would like to move the following curl call to Rcurl:
curl 'http://myserver.org/stream'
-H 'Authorization: Basic XXXXXXXX' -H 'Connection: keep-alive' --data-binary '{"limit": 20}'
-H 'Content-Type: application/json;charset=UTF-8'
This is one of my R tests:
library(RCurl)
url.opts <- list(httpheader = list(Authorization ="Basic XXXXXXXX",
Connection = "keep-alive",
"Content-Type" = "application/json;charset=UTF-8"))
getURLContent('http://myserver.org/stream', .opts=url.opts)
Now I am missing an attribute to add --data or --data-binary. How to add this option?
Thanks a lot
Markus
Thanks a lot #hadley.
This is my working solution:
library(httr)
user <- "test"
password <- "test123"
POST(url = 'http://myserver.org/stream',
config = c(authenticate(user, password, "basic"),
add_headers(Connection = "keep-alive"),
accept_json()),
body = "{'username':'test'}")

Resources