异步POST请求-R,使用RCurl吗? [英] Asynchronous POST Requests - R, using RCurl?

查看:125
本文介绍了异步POST请求-R,使用RCurl吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从R向REST API发出异步请求.以下curl命令说明了传递给api所需的参数.我给你们linux curl命令,因为我希望这可以使它变得清晰起来:

I am trying to make async requests to a REST API from R. The below curl command illustrates the parameters that I need to the pass to the api. I'm giving you guys the linux curl command as I'm hoping that will make it clear:

curl -v -X POST https://app.example.com/api/ \
-H 'Authorization: somepwd' \
-H "Content-Type: application/json" \
-d {key1: value1, key2: value2}

现在,我通过执行以下命令在R中完成相同的操作:

Right now, I'm accomplishing the same thing in R by executing the following:

library(httr)
library(jsonlite)
content(POST('https://app.example.com/api/'
                    ,add_headers(Authorization = 'somepwd') 
                    ,body = toJSON(rDataFrame)
                    ,content_type_json()
             )
        )

目标是从R提交上述POST请求,但更改正文中发送的json字符串,并异步执行.

The goal is to submit the above POST request from R but to vary the json string that is sent in the body, and do that asynchronously.

我一直在寻找可以帮助我发出异步请求而不是顺序发出请求的软件包.我能找到的最接近的东西是RCurl包中的getURIAsynchronous()函数( https://cran.r-project.org/web/packages/RCurl/RCurl.pdf ),但不了解如何使用标头和正文使用其功能提交PUT请求.我真的很想从R发出上述POST请求,但是在URI相同的情况下异步发出,但是对于每个请求发送的数据是不同的.

I have been searching for packages that will help me make asynchronous requests rather than making requests serially. The closest thing I could find is the getURIAsynchronous() function from the RCurl package (https://cran.r-project.org/web/packages/RCurl/RCurl.pdf) but do not understand how to submit a PUT request with headers and a body using their function. I would really like to make the above POST request from R but asynchronously where the URI is the same, but the data sent is different for each request.

我找到了这个 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))
   }
} 

我的想法是我可以将for (i in uris)替换为for(i in jsons),在其中我要遍历要发送到同一URL的不同数据,但是我很难理解RCurl包中的以下概念:

My idea is that I could replace for (i in uris) with for(i in jsons) where I am looping over the different data that I want to send to the same URL, however I am having trouble understanding the following concepts from the RCurl Package:

  1. 如何将标头作为PUT请求的一部分传递.如何在请求正文中传递数据?如上所示,使用httr包非常简单.

我尝试传递curl选项中的标头以及标头.问题是我不知道在哪里传递发帖请求的组成部分:getURIAsynchronous()函数中的身份验证,标头和正文,或者上面已经描述的任何资源.

I tried passing in the header in the curl options and alternatively the header. The thing is I don't understand where to pass the the component parts of the post request: authentication, header, and body within the getURIAsynchronous() function, or any of the resources I have described above.

有人知道如何做到这一点吗?一个例子将非常有用.

Does anyone know how to accomplish this? An example would be incredibly helpful.

推荐答案

curl软件包最近已更新以处理异步请求(

The curl package has been recently updated to handle async requests (see here)

使用curlmagrittrjsonlite软件包,您可以通过以下方式创建异步发布请求:

Using the curl, magrittr and jsonlite packages you can create asynchronous post requests by:

  • 使用handle_setform函数使用标题和正文内容创建通用句柄
  • 编写回调函数以检索结果
  • 初始化池并向其中添加并发请求
  • 通过multi_run
  • 运行游泳池
  • Creating a generic handle with your header and body content using the handle_setform function
  • Writing a call back function to retrieve your results
  • Initializing a pool and adding your concurrent requests to it
  • Running your pool via multi_run

示例代码如下:

library(curl)
library(jsonlite)
library(magrittr)

#create a handle object
h <- new_handle() %>%
handle_setheaders(Authorization = "somepwd",
                  "Content-Type" = "application/json") %>%
  handle_setform(body = toJSON(iris))

pool <- new_pool()
# results only available through call back function
cb <- function(req){cat("done:", req$url, ": HTTP:", req$status, "\n", "content:", rawToChar(req$content), "\n")}

# example vector of uris to loop through
uris <- c("https://app.example.com/api/endpoint1"
          ,"https://app.example.com/api/endpoint2"
          ,"https://app.example.com/api/endpoint3")

# all scheduled requests are performed concurrently
sapply(uris, curl_fetch_multi, done=cb, pool=pool)

# This actually performs requests
out <- multi_run(pool = pool)

这篇关于异步POST请求-R,使用RCurl吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆