[英]Formatting POST body with httr in r
我正在嘗試更改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。
我不包括標題或cookie或其他任何東西。 我也很難找到任何關於此的信息。
非常感謝任何幫助。
好吧,所以吃了一些食物和思考后,快速谷歌制作了這個: 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.