【问题标题】:Curl code to R with postFrom or POST使用 postFrom 或 POST 将代码卷曲到 R
【发布时间】:2018-03-02 13:49:53
【问题描述】:

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

这是我要翻译的卷曲:

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 本身的工作原理。谢谢!

【问题讨论】:

    标签: r curl post rcurl httr


    【解决方案1】:

    我不确定为什么在 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))
               )
    

    【讨论】:

    • 删除了 c(),但不幸的是仍然出现错误:{"campaignKwType":"DESTINATION_LANDMARK","featureId":9849,"guid":"xyz","language":"ENG" ,"lob":"HOTEL","matchType":"EXACT","posa":"GBR","re​​marketingType":"NEW_CUSTOMERS","searchEngine":"GOOGLE","superRegionCampRef":true}
    • 您到底遇到了什么错误?除非我们可以测试,否则很难提供帮助。
    • @Tim496 我错过了您的对象嵌套在数组中的事实。我在您的值列表周围添加了一个额外的未命名列表。试试看。
    猜你喜欢
    • 1970-01-01
    • 2015-10-27
    • 2019-02-25
    • 2012-01-21
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2016-08-12
    相关资源
    最近更新 更多