R:使用curl包上传二进制文件

时间:2016-11-24 20:34:55

标签: r curl rcurl

我想将一些代码从RCurl移植到curl包,看看是否有一些效率提升。

相当于:

library(RCurl)
resp = postForm("https://httpbin.org/post", "fileData" = fileUpload(filename = "app.exe", contentType = "application/octet-stream"),
         .opts = list(httpheader = c(Authorization = "token 123", `Content-Type` = "application/octet-stream")))

curl -H "Authorization: token 123" -H "Content-Type: application/octet-stream" --data-binary @app.exe "https://httpbin.org/post"

使用curl套餐?

顺便说一下,这就是我的尝试:

library(curl)
h=new_handle()
handle_setform(h,
  c(Authorization = "token 123", `Content-Type` = "application/octet-stream"),
  fileData = form_file("app.exe", type="application/octet-stream")
)
req=curl_fetch_memory("http://httpbin.org/post", handle = h)

1 个答案:

答案 0 :(得分:0)

我需要进一步测试,但这似乎是一个通用的上传解决方案:

library(curl)
h=new_handle()
handle_setheaders(h,
        Authorization = "token 123",
        "Content-Type" = "application/x-dosexec"
        )

handle_setform(h,
               app = form_file("app.exe", type="application/x-dosexec"))
req=curl_fetch_memory("http://httpbin.org/post", handle = h)