I am trying to replicate the following in R
curl -XPOST -H 'Content-Type: application/json' -d '{"input":{"webpage/url":"http://http://www.mutualart.com/Exhibitions/Richard-Hamilton/4BC4569A3DCC902A"}}' "https://api.import.io/store/connector/13235010-2b34-4ddb-b8b7-245f12158424/_query?_user=63d1abec-6dde-4387-a5ac-75208005a0cb&_apikey=YOUR_API_KEY"
Under Linux, this works and I get the right output (page URL in the output matches x). But, in R, I get the incorrect output - I get the output for http://www.mutualart.com/Exhibitions/Bailey-s-Stardust/14180718CB6BBBFF
My R Code so far is
x= list(items=c("http://www.mutualart.com/Exhibitions/Richard-Hamilton/4BC4569A3DCC902A"))
headers <- list('Accept' = 'application/json', 'Content-Type' = 'application/json')
uri <- "http://api.import.io/store/connector/13235010-2b34-4ddb-b8b7-245f12158424/_query?_user=63d1abec-6dde-4387-a5ac-75208005a0cb&_apikey=rhq3RoF9oXsoFLwPVfM%2Fkk7ephcJg0bqfbj51PPYhEbkITRm%2Fn23TvtJU4wVWfOkPBjL0sdhZTH5tZg%2Bom72iw%3D%3D"
postForm(uri, .opts=list(postfields=toJSON(x), httpheader=headers))
Related
I want to create an R script to use instead of a curl request.
Using curl, I use a Google API which requires the --data option. I am not sure how to add this option to my curl request using the curl package.
The request that works using Git Bash is as follows:
API_KEY="[xyz]"
curl "https://chromeuxreport.googleapis.com/v1/records:queryHistoryRecord?key=$API_KEY" \
--header 'Content-Type: application/json' \
–-data '{"url": "https://www.example.com/"}'
This is my R code, which doesn't send the necessary URL to return anything good:
library(curl)
API_KEY = "xyz"
h <- new_handle()
handle_setheaders(h,
"Content-Type" = "application/json"
)
r <- curl_fetch_memory(url = paste0("https://chromeuxreport.googleapis.com/v1/records:queryHistoryRecord?key=",API_KEY), h)
So how can I add the --data option using the curl package?
You can use the lower-level curl if you really want to. In this case your original CURL command is performing a POST action with a JSON encoded body. The equivalent of that in the R curl library would be
library(curl)
API_KEY <- "xyz"
url <- paste0("https://chromeuxreport.googleapis.com/v1/records:queryHistoryRecord?key=", API_KEY)
payload <- charToRaw('{"url": "https://www.example.com/"}')
h <- new_handle()
handle_setopt(h, post=TRUE, postfields=payload)
handle_setheaders(h, "Content-Type" = "application/json")
r <- curl_fetch_memory(url = url, h)
cat(rawToChar(r$content))
I am trying to write a script to move repos in a project to another project but I am getting a 400 error whenever I try.
My python requests line looks like:
url = 'https://bitbucketserver.com/rest/api/1.0/projects/example1/repos/repo1'
token = 'TokenString'
response = requests.put(url, headers={'Content-Type': 'application/json', 'Authorization': 'Bearer' + token}, data={'project': {'key': 'NEW_PROJECT'}}, verify=False)
I get a response 400 that says 'Unexpected character ('p' (code112)): expected a valid value (number, string, array, object, true, false, or null) at [Source: com.atlassian.stash.internal.web.util.web.CountingServletInputStream#7ccd7631; line 1, column 2]
I'm not sure where my syntax is wrong
Not python, but work for me via curl:
curl -u 'USER:PASSWORD' --request PUT \
--url 'https://stash.vsegda.da/rest/api/1.0/projects/OLD_PROJECT/repos/REPO_TO_MOVE' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"project": {"key":"NEW_PROJECT"}
}'
Maybe can someone help.
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).
What is the correct way of writing this Curl POST in R?
I would like to have R read the contents of a file as "values" in the post form.
curl -X POST https://api.priceapi.com/jobs \
-d "token=token" \
-d "country=country" \
-d "source=source" \
-d "currentness=currentness" \
-d "completeness=completeness" \
-d "key=key" \
-d 'values=<values>'
So far I have this-
library(RCurl)
library(RJSONIO)
url = "https://api.priceapi.com/jobs"
file.name = ".../output 1 .txt"
results = postForm(url, token="token",
country="country,
source="source",
currentness="currentness",
completeness="completeness,
key="key",
values=fileUpload(filename = file.name))
It returns "Error: Bad Request"
I also tried it using httr post request-
r = POST(url, body = list(token="token",
country="country,
source="source",
currentness="currentness",
completeness="completeness,
key="key",
values=upload_file(file.name)) )
Here upload_file is not uploading the contents of the file but, I am guessing it is passing the path to the file (as a string) into the "values" parmeter.
Naturally that does not return the correct results.
The result of the httr POST request is;
Response [https://api.priceapi.com/jobs]
Date: 2016-12-13 10:11
Status: 400
Content-Type: application/json; charset=utf-8
Size: 228 B
{
"success": false,
"reason": "parameter value invalid",
"parameter": "value",
"valid values": "An array or a string containing values separated by newline",
"comment": "Make sure the parameter 'value' has a valid value!"
I could solve this by using
file=readLines(".../output 1.txt")
inputValues <- paste(file,collapse="\n")
and then passing inputValues in the values parameter.
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"))