Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing a JSON object through POST using Python
    primarykey
    data
    text
    <p>I'm trying to post a JSON object through a POST. I'm trying to do it as follows:</p> <pre><code>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' </code></pre> <p>The data is then received on the other end (I'm using Google App Engine) with:</p> <pre><code>point_data = json.load(self.request.get('point_data')) </code></pre> <p>But I get the following error:</p> <pre><code>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 - </code></pre> <p>Any ideas on how to fix this?</p> <p><strong>EDIT:</strong> As requested here is an example of the points:</p> <pre><code>(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' } } </code></pre> <p><strong>EDIT 2:</strong> Thanks to Daniel Roseman and Nick Johnson who both caught my error. I've changed</p> <pre><code>point_data = json.loads(self.request.get('point_data')) </code></pre> <p>This has solved the error but, now I'm getting:</p> <pre><code>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 - </code></pre> <p>which relates to the following code:</p> <pre><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(): ... </code></pre> <p>Any ideas on this one?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload