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

时间:2018-03-02 13:49:53

标签: r curl post rcurl httr

我试图使用postForm和amp;来掩盖一些卷曲代码。 R中的POST方法遵循其他一些示例,但是,我遇到了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))
               )

会感谢任何指针,我已经使用其他问题作为这两个请求的示例,我也检查了卷曲本身的工作原理。谢谢!

1 个答案:

答案 0 :(得分:0)

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

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))
           )