在Coldfusion中保存JSON响应

时间:2018-12-27 17:06:27

标签: json coldfusion

我想使用Firebase Cloud Messaging发送推送消息。除了一件事,一切都在工作。我想保存Firebase的响应(请参阅下文)以更新数据库中的用户配置文件。因此,假设响应返回失败,我想将该响应发送回我的数据库。

要发送推送消息,请使用以下脚本:

var key = 'my-key';
var to = 'to-key';
var notification = {
   'title': 'Portugal vs. Denmark',
   'body': '5 to 1',
   'icon': 'firebase-logo.png',
   'click_action': 'http://localhost:8081'
};

fetch('https://fcm.googleapis.com/fcm/send', {
   'method': 'POST',
   'headers': {
   'Authorization': 'key=' + key,
   'Content-Type': 'application/json'
},
'body': JSON.stringify({
'notification': notification,
'to': to
 })
 }).then(function(response) {
      console.log(response);
 }).catch(function(error) {
     console.error(error);
 })

我从Firebase得到的响应是: response from firebase

我的问题是如何保存(或发送)该响应到我的Coldfusion服务器。我正在考虑将脚本重新编写为Coldfusion,例如:

 <cfscript>
 objResponse = {
  'message':{
         'to':'SOME_TOKEN',
 'notification':{
    'title': 'Portugal vs. Denmark',
    'body': '5 to 1',
    'icon': 'firebase-logo.png',
    'click_action': 'localhost:8081'
   }
   }
  }
  </cfscript> 

  <Cfdump var="#objResponse#" >
  <cfoutput >#SerializeJSON(objResponse)#</cfoutput>

  <cfhttp url="https://fcm.googleapis.com/fcm/send" method="post" result="objGet">
   <cfhttpparam type="header" name="Accept" value="application/json" />
   <cfhttpparam type="header" name="Authorization" value="key=MY_KEY">
   <cfhttpparam type="header" name="Content-Type" value="application/json" />
   <cfhttpparam type="body"   value='#SerializeJSON(objResponse)#'/>
  </cfhttp>

但这给了我400个错误的请求:

enter image description here

https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream上我发现 仅适用于JSON请求。指示无法将请求解析为JSON,或者请求包含无效字段(例如,传递期望数字的字符串)。响应中描述了确切的失败原因,应在重试请求之前解决该问题。

因此,我知道它与我发送的JSON请求有关,但我无法弄清楚问题出在哪里。

1 个答案:

答案 0 :(得分:3)

由于您是通过浏览器中的javascript发出此请求的,因此您需要在.then()回调中添加一些代码,这些代码会向您的ColdFusion服务器发出ajax发布请求,从而发送要保存的数据。

不确定您的流程在这里,但是您也可以从ColdFusion服务器本身发出http请求。