Asynchronous POST Requests - R, using RCurl? -
i trying make async requests rest api r. below curl command illustrates parameters need pass api. i'm giving guys linux curl command i'm hoping make clear:
curl -v -x post https://app.example.com/api/ \ -h 'authorization: somepwd' \ -h "content-type: application/json" \ -d {key1: value1, key2: value2}
right now, i'm accomplishing same thing in r executing following:
library(httr) library(jsonlite) content(post('https://app.example.com/api/' ,add_headers(authorization = 'somepwd') ,body = tojson(rdataframe) ,content_type_json() ) )
the goal submit above post request r vary json string sent in body, , asynchronously.
i have been searching packages me make asynchronous requests rather making requests serially. closest thing find geturiasynchronous() function rcurl package (https://cran.r-project.org/web/packages/rcurl/rcurl.pdf) not understand how submit put request headers , body using function. make above post request r asynchronously uri same, data sent different each request.
i found http://www.omegahat.org/rcurl/concurrent.html
geturis = function(uris, ..., multihandle = getcurlmultihandle(), .perform = true) { content = list() curls = list() for(i in uris) { curl = getcurlhandle() content[[i]] = basictextgatherer() opts = curloptions(url = i, writefunction = content[[i]]$update, ...) curlsetopt(.opts = opts, curl = curl) multihandle = push(multihandle, curl) } if(.perform) { complete(multihandle) lapply(content, function(x) x$value()) } else { return(list(multihandle = multihandle, content = content)) } }
my idea replace for (i in uris)
for(i in jsons)
looping on different data want send same url, having trouble understanding following concepts rcurl package:
how pass header part of put request. how pass data in body of request? pretty straight forward using
httr
package have illustrated above.i tried passing in header in curl options , alternatively header. thing don't understand pass the component parts of post request: authentication, header, , body within
geturiasynchronous()
function, or of resources have described above.
does know how accomplish this? example incredibly helpful.
the curl
package has been updated handle async requests (see here)
using curl
, magrittr
, jsonlite
packages can create asynchronous post requests by:
- creating generic handle header , body content using
handle_setform
function - writing call function retrieve results
- initializing pool , adding concurrent requests it
- running pool via
multi_run
example code below:
library(curl) library(jsonlite) library(magrittr) #create handle object h <- new_handle() %>% handle_setheaders(authorization = "somepwd", "content-type" = "application/json") %>% handle_setform(body = tojson(iris)) pool <- new_pool() # results available through call function cb <- function(req){cat("done:", req$url, ": http:", req$status, "\n", "content:", rawtochar(req$content), "\n")} # example vector of uris loop through uris <- c("https://app.example.com/api/endpoint1" ,"https://app.example.com/api/endpoint2" ,"https://app.example.com/api/endpoint3") # scheduled requests performed concurrently sapply(uris, curl_fetch_multi, done=cb, pool=pool) # performs requests out <- multi_run(pool = pool)
Comments
Post a Comment