尝试将JSON HTTP帖子转换为CFHTTP帖子

Sco*_*ott 1 coldfusion cfhttp firebase cfml firebase-cloud-messaging

我正在尝试通过重新创建成功的Firebase控制台帖子的输出来通过CFHTTP正确编码对Firebase Cloud Messaging 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"
  }
} 
Run Code Online (Sandbox Code Playgroud)

这是我们当前的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>
Run Code Online (Sandbox Code Playgroud)

处理formfield时似乎会出现此问题。我收到以下错误,该错误在处理第一个表单字段“至”时发生。

JSON_PARSING_ERROR:位置0处的意外字符(t)。

任何帮助将不胜感激。谢谢!!!

Age*_*eax 5

API希望将JSON字符串作为请求正文,但是代码将分别提交所有值,而不是表单字段。摆脱所有formfield参数,并使用适当的键和值构建单个结构:

<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"
                      }
                } >
Run Code Online (Sandbox Code Playgroud)

然后将其序列化为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>
Run Code Online (Sandbox Code Playgroud)