如何将此curl命令转换为R curl调用?

时间:2019-06-20 02:27:08

标签: r curl

我有这个curl命令,可以在bash中调用

curl -X POST -H 'Content-Type: text/csv' --data-binary @data/data.csv https://some.url.com/invocations > data/churn_scored.jsonl

它将CSV文件发布到API端点,然后将结果重定向到.jsonl文件中。

因此,在其中找不到可以使用curl的@来指定要发布到端点的数据文件的位置。

使用R's curl包(或任何其他包)来实现CURL发布的方法是什么?我可以用另一种方式找出输出的重定向。

2 个答案:

答案 0 :(得分:2)

这是将curl命令转换为其他语言的非常有用的网站:https://curl.trillworks.com/#r

在插入curl命令时,我得到了。

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
       ContentView()
    }
}
#endif

答案 1 :(得分:1)

针对特定符号@。来自man curl

--data-binary <data>
  (HTTP) This posts data exactly as specified with no extra processing whatsoever.
  If you start the data with the letter @, the rest should be a filename.  Data is
  posted in a similar manner as --data-ascii does, except that newlines are preserved
  and conversions are never done.

  If this option is used several times, the ones following the first will append data
  as described in -d, --data.

似乎无需担心@

@ chinsoon12提到,httr是处理请求的好方法:

  • -X--request转换为VERB函数POST(),其中包括--data-binary
  • -H--header转换为add_headers(),但是有用于设置内容类型的特殊功能(请参见下文)

它看起来像:

library(httr)
response <- POST(
      url = "https://some.url.com/invocations",
      body = upload_file(
        path =  path.expand("data/data.csv"),
        type = 'text/csv'),
      verbose()
    )
# get response and write to you disk
相关问题