I have been trying to send messages to the GCM server using python urllib2
. But it has been returning Error 400
(suggesting Malformed JSON
). This is a very similar question to Google Cloud Messaging HTTP Error 400: Bad Request but I have already tried the solution provided there, which did not work for me.
Please find my code below
idarr = []
for obj in objects:
idarr.append(str(obj["gcid"]))
key = "AIzaxxxxxxL9xxxxxxxxxxxxxxxxxxxWbk"
data = { "registration_ids": idarr , "data": "on" }
data = json.dumps(data)
headers = {"Content-Type":"application/json","Authorization": "key="+key}
request = urllib2.Request('https://android.googleapis.com/gcm/send',data,headers )
f=urllib2.urlopen(request)
Also I am presenting here the json I am posting in the data field
{
"data": "on",
"registration_ids": [
"APA91bEPvyQuqRndnwZ94YxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxnmFVPy6nv4mPU5UyhDEZ7Jha9HnrV9nXQf25ojs8Hc_-Bw8-ElJ320koeL0PGL6hnWj0xuyltozoND_x",
"APA91bEPvyQxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxVPy6nv4mPU5UyhDEZ7Jha9HnrV9nXQf25ojs8Hc_-Bw8-ElJ320koeL0PGL6hnWj0xuyltozoND_x"
]
}