使用 postFrom 或 POST 将代码卷曲到 R

Curl code to R with postFrom or POST

我尝试使用 R 中的 postForm 和 POST 方法根据此处的其他一些示例来隐藏一些 curl 代码,但是,我遇到了 400 错误。

这是我要翻译的 Curl:

curl -X POST --header 'Content-Type: application/json;charset=UTF-8' --
header 'Accept: application/json' -d '[
  {

"campaignKwType":"DESTINATION_LANDMARK",
"featureId": 9849,
"guid": "xyz",
"language": "ENG",
"lob": "HOTEL",
"matchType": "EXACT",
"posa": "GBR",
"remarketingType": "NEW_CUSTOMERS",
"searchEngine": "GOOGLE",
"superRegionCampRef": true
 } ]' 'https://apiaddress'

使用 postForm:

   x <- postForm("https://apiaddress",
         .opts = list(postfields = toJSON(list(campaignKwType = "DESTINATION_LANDMARK", featureId = 9849,  guid = "xyz", language = "ENG", lob = "HOTEL", matchType = "EXACT", posa = "GBR", remarketingType = "NEW_CUSTOMERS", searchEngine = "GOOGLE", superRegionCampRef = TRUE)),
                      httpheader = c('Content-Type' = 'application/json;charset=UTF-8', Accept = 'application/json')))

与POST:

x <- POST("https://apiaddress",
               verbose(),
               encode = "json",
               body = list(c(campaignKwType = "DESTINATION_LANDMARK", featureId = 9849,  guid = "xyz", language = "ENG", lob = "HOTEL", matchType = "EXACT", posa = "GBR", remarketingType = "NEW_CUSTOMERS", searchEngine = "GOOGLE", superRegionCampRef = TRUE))
               )

不胜感激,我已经使用其他问题作为这两个请求的示例,并且我还检查了 curl 本身是否有效。谢谢!

我不确定为什么 POST 版本的 list() 中有 c(),但我想这不是您想要的。只需使用

x <- POST("https://apiaddress",
           verbose(),
           encode = "json",
           body = list(list(campaignKwType = "DESTINATION_LANDMARK", featureId = 9849,  guid = "xyz", language = "ENG", lob = "HOTEL", matchType = "EXACT", posa = "GBR", remarketingType = "NEW_CUSTOMERS", searchEngine = "GOOGLE", superRegionCampRef = TRUE))
           )