0

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"
    ]
} 
Community
  • 1
  • 1
Abhilash PS
  • 734
  • 1
  • 11
  • 25
  • Well, your JSON is fine, so that's not your problem. The other question had no accepted answer, and you have *plenty* of reputation to comment on the other question (50 points is all it takes). – Martijn Pieters Nov 05 '12 at 11:35
  • @MartijnPieters Sorry, A small confusion from my part. Thanks – Abhilash PS Nov 05 '12 at 11:40
  • 1
    Does `print f.read()` show anything useful? Perhaps Google provided you with a more detailed error message. Also inspect the headers (`print f.info().headers`) to see if there are any clues there as to what might be wrong. – Martijn Pieters Nov 05 '12 at 11:44
  • I am actually getting a python module error. urllib2.HTTPError: HTTP Error 400: Bad Request – Abhilash PS Nov 05 '12 at 11:57
  • 1
    Of course, it's an exception. The exception has the same information though; use `try: f=urllib2.urlopen(request)`, `except urllib2.HTTPError, e: print e.hdrs, e.read()`. See http://docs.python.org/2/library/urllib2.html#urllib2.HTTPError for more information on the exception. – Martijn Pieters Nov 05 '12 at 12:02

0 Answers0