I'm trying to post a JSON object through a POST. I'm trying to do it as follows:
import json, urllib, urllib2
filename = 'test.json'
race_id = 2530
f = open(filename, 'r')
fdata = json.loads(f.read())
f.close()
prefix = 'localhost:8000'
count = 0
for points in fdata['positions'].iteritems():
print '--' + str(count) + '--------'
url = 'http://'+prefix+'/api/points'
parameters = {'point_data': json.dumps(points), 'race_id': race_id}
data = urllib.urlencode(parameters)
print data
request = urllib2.Request(url, data)
response = urllib2.urlopen(request)
count += 1
break;
print 'Finished adding points'
The data is then received on the other end (I'm using Google App Engine) with:
point_data = json.load(self.request.get('point_data'))
But I get the following error:
ERROR 2010-06-30 15:08:05,367
__init__.py:391] 'unicode' object has no attribute 'read' Traceback (most
recent call last): File
"/home/ian/workspace/google_appengine/google/appengine/ext/webapp/__init__.py",
line 513, in __call__
handler.post(*groups) File "/home/ian/workspace/codebase/track_builder/geo-api.py",
line 276, in post
point_data = json.load(self.request.get('point_data'))
File
"/home/ian/workspace/google_appengine/lib/django/django/utils/simplejson/__init__.py",
line 208, in load
return cls(encoding=encoding, **kw).decode(fp.read()) AttributeError: 'unicode' object has
no attribute 'read' INFO
2010-06-30 15:08:05,376
dev_appserver.py:3266] "POST
/api/points HTTP/1.1" 500 -
Any ideas on how to fix this?
EDIT: As requested here is an example of the points:
(u'1276859700',
{
u'24': {
u'tempc': u'-22.7',
u'gpsq': u'1',
u'altm': u'65527',
u'hd': u'112',
u'hdop': u'0.93',
u'bton': u'0',
u'maxv': u'20.15',
u'idit': u'1',
u'satc': u'10',
u'minv': u'20.15',
u'lat': u'35.271993',
u'btusr': u'0',
u'lng': u'-121.845353',
u'knots': u'7'
},
u'11': {
u'tempc': u'13.0',
u'gpsq': u'1',
u'altm': u'65535',
u'hd': u'130',
u'hdop': u'0.84',
u'bton': u'0',
u'maxv': u'15.96',
u'idit': u'1',
u'satc': u'12',
u'minv': u'15.88',
u'lat': u'34.877815',
u'btusr': u'0',
u'lng': u'-121.386116',
u'knots': u'8'
}
}
EDIT 2: Thanks to Daniel Roseman and Nick Johnson who both caught my error. I've changed
point_data = json.loads(self.request.get('point_data'))
This has solved the error but, now I'm getting:
ERROR 2010-06-30 16:07:29,807 __init__.py:391] 'list' object has no attribute 'iteritems'
Traceback (most recent call last):
File "/home/ian/workspace/google_appengine/google/appengine/ext/webapp/__init__.py", line 513, in __call__
handler.post(*groups)
File "/home/ian/workspace/codebase/track_builder/geo-api.py", line 255, in post
for time, units in point_data.iteritems():
AttributeError: 'list' object has no attribute 'iteritems'
INFO 2010-06-30 16:07:29,816 dev_appserver.py:3266] "POST /api/points HTTP/1.1" 500 -
which relates to the following code:
class TrackPoint(webapp.RequestHandler):
def post(self):
point_data = json.loads(self.request.get('point_data'))
race_id = self.request.get('race_id')
added = []
failed = []
for time, units in point_data.iteritems():
for unit, data in units.iteritems():
...
Any ideas on this one?