在 r 中使用 httr 格式化开机自检正文



>我正在尝试更改 hubspot 中的联系人属性值。

文档:https://developers.hubspot.com/docs/methods/contacts/update_contact

此项目位于一系列以 JSON 编码的数据帧中(请参阅下面的 GET 请求(

我尝试了几种格式

1( 遵循 GET 请求格式

library(httr)
library(jsonlite)
    URL <- paste0('https://api.hubapi.com/contacts/v1/contact/vid/',VID,'/profile?hapikey=',hapikey, sep = "")
    GURL <- GET(url = URL)
Contact <- content(URL, as = "text")
Contact <- fromJSON(Contact)
Contact$properties$needs_statements$value
#returns
[1] "Yes"
#so then working backwards in the POST request:
body <- toJSON('No', content$properties$property$needs_statements$value)
#alternatively
body <- list('No', content$properties$property$needs_statements$value)
#alternatively 
body <- ('No', content$properties$property$needs_statements$value)
#Post Request
POST( url = URL, body = body, encode = "json")

2(尝试遵循文档中的python格式

library(httr)
body <- '{
  "properties": [
    {
      "property": "needs_statements",
      "value": "No"]}
}'
#alternatively
body <- toJSON('{
  "properties": [
    {
      "property": "needs_statements",
      "value": "No"
      }
     ]
    }')
#Post Request
POST( url = URL, body = body, encode = "json")

我也试过encode = "raw" encode = "form"

这些都是拉回代码 400,指示请求正文中存在错误。

拍摄204。

我没有包括标题或饼干或其他任何东西。我也很难找到这方面的任何信息。

任何帮助都非常感谢。

好的,所以在吃了一些食物并思考之后,快速谷歌产生了这个:https://cran.r-project.org/web/packages/jsonlite/vignettes/json-aaquickstart.html

使用可用于执行此操作的测试:

fromJSON('[{"name":"Erik", "age":43}, {"name":"Anna", "age":32}]')

打印数据框。

 name age
1 Erik  43
2 Anna  32

对我来说,棘手的部分是需要获得与原始GET请求相同的结构。

(我试图构建数据帧的数据框,但进展不顺利(

然后我想到了上面的测试,并认为我可以在 JSON 上做同样的测试。我做到了,并创建了一个元素。

x <- fromJSON('{
  "properties": [
    {
      "property": "needs_statements",
      "value": "No"
    }
    ]
}')

和动臂:204

相关内容

最新更新