1

I am trying to properly code a call to the Firebase Cloud Messaging REST API though CFHTTP, by recreating the output of successful Firebase console post. Below is what the console states is the proper code

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"
  }
} 

This is our current CFHTTP code:

<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>

The problem seems to occur when the formfields are processed. I am getting the error below, which occurs when the first formfield "to" is processed.

JSON_PARSING_ERROR: Unexpected character (t) at position 0.

Any help would be greatly appreciated. Thank you!!!

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Scott
  • 459
  • 2
  • 10
  • 1
    This post might be helpful: https://stackoverflow.com/questions/52820121/how-can-we-pass-the-json-data-in-http-request-body-in-form-post/52829428#52829428 – Redtopia Sep 25 '19 at 03:38

1 Answers1

6

The API is expecting a JSON string as the request body, but the code is submitting all of the values separately, as form fields instead. Get rid of all the formfield parameters and build a single structure with the appropriate keys and values:

<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"
                      }
                } >

Then serialize it as JSON and send it using 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>
SOS
  • 6,430
  • 2
  • 11
  • 29