我正在使用HTTR包(使用带有json编码的POST函数)通过SendGrid API从Shiny应用程序发送电子邮件。我需要在用于定义电子邮件文本正文的json引号之间传递R对象:
我尝试将R对象转换为json,如下所示:
client_id<- "f432jj"
email_text<- paste("Below is your unique key:", client_id, "Please copy
your key to the clipboard, then click 'Begin'")
email_text<- jsonlite::toJSON(email_text)
这是我需要在其中插入email_text对象的json代码。
body = '{"from": {"email":"xxx@gmail.com"},
"personalizations": [{"to": [{"email":"zzz@gmail.com"}],
"dynamic_template_data":{
"header":"A measure is ready to be completed",
"text": email_text,
"c2a_button":"Begin",
"c2a_link":"yyy@gmail.com"}}],
"template_id":"e-98766334"}'
当尝试如上所述传递email_text对象并发送电子邮件时,我得到:
HTTP/1.1 400 Bad Request
我认为这意味着语法错误。
非常感谢任何帮助。
答案 0 :(得分:0)
通常,您不会将主体JSON数据构建为字符串。您可以构建代表数据的列表,然后让jsonlite为您将其转换为JSON字符串。您的示例可能看起来像这样
client_id<- "f432jj"
email_text<- paste("Below is your unique key:", client_id, "Please copy
your key to the clipboard, then click 'Begin'")
body <- list(
from = list(email="xxx@gmail.com"),
personalizations = list(list(to=list(list(email="zzz@gmail.com")))),
dynamic_template_data = list(
header="A measure is ready to be completed",
text = email_text,
c2a_button = "Begin",
c2a_link = "yyy@gmail.com"
),
template_id = "e-98766334"
)
jsonlite::toJSON(body,auto_unbox=TRUE)