pedroooo pedroooo - 1 month ago 21
JSON Question

JSON_PARSING_ERROR: Unexpected character (t) at position 1

I have a method in ROR:

params = "{'to':'#{registration_ids}','notification':{'body':'#{message}'} 'priority':'10'}"

system("curl -X POST --header 'Authorization: key=MY_KEY' --Header 'Content-Type: application/json' https://fcm.googleapis.com/fcm/send -d '#{params}'")


i recieve message: JSON_PARSING_ERROR: Unexpected character (t) at position 1

Answer

Your JSON is invalid - I would suggest using jsonlint in the future.

require 'json'
params = {to: registration_ids, notification: {body: message}, priority:10}.to_json
system("curl -X POST --header 'Authorization: key=MY_KEY' --Header 'Content-Type: application/json' https://fcm.googleapis.com/fcm/send -d '#{params}'")

You are missing a , between notification and priority. Also json should really have double quotes (see JSON standards) and you can do that by running the .to_json method on the hash...

Comments