Unable to upload to Watson Analytics data with R - r

I have the following code:
curl -v -X PUT -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:text/csv" https://api.ibm.com/watsonanalytics/run/data/v1/datasets/ID_OF_DATA_SET/content -d "YOUR_DATA"
That code is the example they gave me in order to upload data, when I try to translate the to R it looks like this:
I am using the following libraries:
library(RCurl)
library(RJSONIO)
library(XML)
library(httr)
library(readr)
and the code looks like :
Upload_data<-PUT( url = "https://api.ibm.com/watsonanalytics/run/data/v1/datasets/DataID/content",
add_headers('Content-Type' = 'text/csv'),
add_headers('Accept'= 'application/json'),
add_headers('X-IBM-Client-Secret' = Secret),
add_headers('X-IBM-Client-Id' = Client),
add_headers('Authorization: Bearer'=Auth, body= "Province,Population density,Area,Population,
Ontario,97,83858,8169929
Quebec,337,30510,11007020
Alberta,111,547030,63601002
Manitoba,233,357021,81799600
British Columbia,393,41526,16824400
Costa Rica, 1,10000, 1000000"))
Seems that it is not working, do you know some way to fix it?

If I'm not mistaken, I believe that R is not yet supported through the Watson Analytics API Explorer, which is what your PUT request is using "https://api.ibm.com/watsonanalytics/"
The supported languages include Python, JavaScript, Java, C#, and PHP.

Related

R translating curl commands to post multipart form - problem

I have the following curl command that, when run from command line, works perfectly:
curl -X POST -u "myusername|myemail#domain.com:myPassword"
-H "Content-Type: multipart/form-data"
--form file=#MyFileForUploading.csv
https://mysite-data.herokuapp.com/api/mymarket/setups/uploads
[Apologies: this is not a working example as I cannot provide the real url and credentials. I am hoping you can help me with the translation from curl to httr without running the example yourselves.]
Here's my attempt to translate the above to the language of R's httr, which did NOT work:
library(httr)
POST("https://mysite-data.herokuapp.com/api/mymarket/setups/uploads",
config = authenticate("myusername|myemail#domain.com", "myPassword"),
body = upload_file("MyFileForUploading.csv", type = "text/csv"),
encode = "multipart")
The curl command serves to upload a csv file being used as setup for a web-based trading interface. Setup includes things like trader initial holdings of objects, trader permissions (to buy and sell), etc. All this is simply stored as a csv file (columns = setup parameters; rows = traders).
Can anyone see an obvious mis-translation? I am very ignorant about both curl and httr. My translation is based on learning from examples and I wouldn't be surprised if there's an obvious failure, for example, with the content-type part of the command.
Thanks!
You're really close. This works with environment values setup in "~/Renviron":
library("httr")
post_url <- Sys.getenv("POST_URL")
username <- Sys.getenv("USERNAME")
password <- Sys.getenv("PASSWORD")
csv_file <- Sys.getenv("CSV_FILE")
POST(
url = post_url,
config = authenticate(username, password),
body = list(file = upload_file(csv_file)),
encode = "multipart",
verbose()
) -> response
The key is the file = as you used in your CURL command.

Adding comment to a JIRA issue ticket using REST API in R

I would like to add more comments to an existing JIRA ticket using REST API pro grammatically from R
I tried the following based on this link in this forum but did not work:
library(httr)
POST("https://xxxxxx.atlassian.net/rest/api/2/issue/issueId/comment",body = "New Comment", authenticate(userid,password, "basic"))
I checked the accepted answer in your forum link, which suggests the following POST using curl:
curl -u admin:admin -X POST --data '{"body": "comment."}' -H "Content-type: application/json" http://localhost:2990/jira/rest/api/2/issue/TEST-1/comment
Note carefully that the body is JSON content, with the content type also being set as JSON. You may try doing the same in your call to the POST() function from httr:
POST("https://xxxxxx.atlassian.net/rest/api/2/issue/issueId/comment",
body = '{"body": "comment."}', authenticate(userid, password, "basic"),
encode="raw")

curl ignore --data starting with # sign: don't read from file

In slack you can script slackbot to post messages to a channel like this:
curl --data "$msg" $'https://<yourteam>.slack.com/services/hooks/slackbot?token=<yourtoken>&channel=#random'
Now i'd like to mention a username as the first part of the message like msg="#joernhees hello self".
The problem with this is that if the --data argument of curl starts with an # sign it will interpret the string after the # as filename and post its content. Is there a way to make curl ignore the # sign and to send a literal # as the first char of a post request?
If you are on a new version of cURL you can also use the --data-raw option:
http://curl.haxx.se/docs/manpage.html#--data-raw
A word of warning is that looking my laptop it appears Yosemite ships with an older version of cURL.
In general if you're creating tools to post to Slack I'd recommend using an HTTP library in your script rather than calling out to a shell and invoking the curl command.
Actually i just found out i can do this (not sure it's the best option though):
curl --data '#-' $'https://<yourteam>.slack.com/services/hooks/slackbot?token=<yourtoken>&channel=#random' <<< "$msg"
The trick is to tell curl to read from stdin #- and then pass the message in via that.

What is the RCurl format to transfer a data file?

I am a bit puzzled because I can not get this to work from within R.
I want to execute the following Curl request from within R using RCurl:
curl -T 'temp.csv' -H 'Content-Type: text/csv' https://api.crowdflower.c.json?key={APIKEY}
That is, I want to transfer the file "temp.csv" to the server. This curl command itself works really well, however, I would like to send it from within R without having to use
system("curl -T 'temp.csv' -H 'Content-Type: text/csv' https://api.crowdflower.c.json?key={APIKEY}")
I have played around with the fileUpload() function, but it does not work.
Do you have any clues or hints for me how I can get this to work with RCurl? Maybe it is not really designed to be able to handle this as I see that most people use it only to automate forms or to scrape websites using R.
Thanks a lot for your help,
best Thiemo

How to translate the following simple curl call to R code using RCurl?

Consider the following commands (2 examples)
curl -H "Accept:text/plain" http://rxnav.nlm.nih.gov/REST/rxcui/866350/allrelated
curl -i -L -H "Accept: application/xml" http://pub.orcid.org/search/orcid-bio?q=digital-object-ids:"10.1088/0031-9155/58/3/535"
The user is able to run those on win7 command line with curl.exe installed. The RCurl package has no vignette and it has a lot of commands to choose from. How do I get the text file and XML file output using RCurl?
I think you can use the following :
mytxt <- getURL("http://rxnav.nlm.nih.gov/REST/rxcui/866350/allrelated", httpheader=c(Accept="text/plain"))
And :
myxml <- getURL('http://pub.orcid.org/search/orcid-bio?q=digital-object-ids:"10.1088/0031-9155/58/3/535"', httpheader=c(Accept="application/xml"))
You will find many examples in the getURL help page.

Resources