尝试将 JSON HTTP post 转换为 CFHTTP post
Trying to convert JSON HTTP post to CFHTTP post
我正在尝试通过重新创建成功的 Firebase 控制台 post 的输出,通过 CFHTTP 正确编码对 Firebase 云消息传递 REST API 的调用。以下是控制台显示的正确代码
POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Content-Type: application/json
Authorization: key=AIzcXE
cache-control: no-cache
{
"to": "e5kpn8h9bR95NuXVHTOi50bCURG0BS4S6ccUm3X5q",
"priority": "high",
"notification" : {
"title": "",
"body" : "This is the actual message content",
"sound": "default",
"image": "https://gladevalleyanimalhospital.net/wp-content/uploads/2017/03/raster-7.png"
}
}
这是我们当前的 CFHTTP 代码:
<cfhttp method="Post" url="https://fcm.googleapis.com/fcm/send">
<cfhttpparam type="header" name="Authorization" value="key=AIzXE">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="header" name="Postman-Token" value="e19b8abf3f9">
<cfhttpparam type="header" name="cache-control" value="no-cache">
<cfhttpparam type="Formfield" value="3569D24982E3B" name="to">
<cfhttpparam type="Formfield" value="high" name="priority">
<cfhttpparam type="Formfield" value="Test" name="title">
<cfhttpparam type="Formfield" value="This is the actual message content" name="body">
<cfhttpparam type="Formfield" value="https://gladevalleyanimalhospital.net/wp-content/uploads/2017/03/raster-7.png" name="image">
</cfhttp>
问题似乎是在处理表单域时出现的。我收到以下错误,这是在处理第一个表单域 "to" 时发生的。
JSON_PARSING_ERROR: Unexpected character (t) at position 0.
如有任何帮助,我们将不胜感激。谢谢!!!
API 期望 JSON 字符串作为请求 body,但代码将所有值分别提交为表单字段.摆脱所有的表单域参数并使用适当的键和值构建单个结构:
<cfset bodyData = { "to": "***the_message_recipient_id_here****",
"priority": "high",
"notification" : {
"title": "",
"body" : "This is the actual message content",
"sound": "default",
"image": "https://example.com/someimage-name.png"
}
} >
然后序列化为JSON并使用type="body"
发送:
<cfhttp method="Post" url="https://fcm.googleapis.com/fcm/send">
<cfhttpparam type="header" name="Authorization" value="key=AIzXE">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="header" name="cache-control" value="no-cache">
<cfhttpparam type="body" value="#serializeJSON( bodyData )#">
</cfhttp>
我正在尝试通过重新创建成功的 Firebase 控制台 post 的输出,通过 CFHTTP 正确编码对 Firebase 云消息传递 REST API 的调用。以下是控制台显示的正确代码
POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Content-Type: application/json
Authorization: key=AIzcXE
cache-control: no-cache
{
"to": "e5kpn8h9bR95NuXVHTOi50bCURG0BS4S6ccUm3X5q",
"priority": "high",
"notification" : {
"title": "",
"body" : "This is the actual message content",
"sound": "default",
"image": "https://gladevalleyanimalhospital.net/wp-content/uploads/2017/03/raster-7.png"
}
}
这是我们当前的 CFHTTP 代码:
<cfhttp method="Post" url="https://fcm.googleapis.com/fcm/send">
<cfhttpparam type="header" name="Authorization" value="key=AIzXE">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="header" name="Postman-Token" value="e19b8abf3f9">
<cfhttpparam type="header" name="cache-control" value="no-cache">
<cfhttpparam type="Formfield" value="3569D24982E3B" name="to">
<cfhttpparam type="Formfield" value="high" name="priority">
<cfhttpparam type="Formfield" value="Test" name="title">
<cfhttpparam type="Formfield" value="This is the actual message content" name="body">
<cfhttpparam type="Formfield" value="https://gladevalleyanimalhospital.net/wp-content/uploads/2017/03/raster-7.png" name="image">
</cfhttp>
问题似乎是在处理表单域时出现的。我收到以下错误,这是在处理第一个表单域 "to" 时发生的。
JSON_PARSING_ERROR: Unexpected character (t) at position 0.
如有任何帮助,我们将不胜感激。谢谢!!!
API 期望 JSON 字符串作为请求 body,但代码将所有值分别提交为表单字段.摆脱所有的表单域参数并使用适当的键和值构建单个结构:
<cfset bodyData = { "to": "***the_message_recipient_id_here****",
"priority": "high",
"notification" : {
"title": "",
"body" : "This is the actual message content",
"sound": "default",
"image": "https://example.com/someimage-name.png"
}
} >
然后序列化为JSON并使用type="body"
发送:
<cfhttp method="Post" url="https://fcm.googleapis.com/fcm/send">
<cfhttpparam type="header" name="Authorization" value="key=AIzXE">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="header" name="cache-control" value="no-cache">
<cfhttpparam type="body" value="#serializeJSON( bodyData )#">
</cfhttp>