【问题标题】:R: How to pass array in the POST request with form encoding?R:如何使用表单编码在 POST 请求中传递数组?
【发布时间】:2021-02-02 12:35:26
【问题描述】:

发布请求如下所示: curl --location --request POST ''
--form 'ids=["ae8a1312","59569d79"]'

这里的 ids 需要在 post 请求中作为数组传递。尝试使用以下代码:

body <- list(
        sweep_ids = ids
      )
resp <- POST(url, body = body, encode = "form")

这里的变量ids 是id 列表。上面这段代码给出了以下错误

Warning: Error in vapply: values must be length 1,
 but FUN(X[[1]]) result is length 2

不能将编码用作 JSON。

如何在 R 中将 id 列表传递给这个表单编码的 POST 请求?

【问题讨论】:

  • 试试POST(url, body = '{"a":"ae8a1312","b":"59569d79"}', encode = "raw")。还是不行?

标签: r post shiny httr


【解决方案1】:
    l <- paste('["',paste(ids, collapse='","' ),'"]', sep="")

    body <- list(
        sweep_ids = l
      )

   resp <- POST(url, body = body, encode = "form")

将列表折叠为文本

【讨论】:

    猜你喜欢
    • 2015-08-20
    • 2017-12-11
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    相关资源
    最近更新 更多