How to parse a POST argument to a REST service?

It seems I have another JSON problem, this time when sending to a REST service. I am using Flask-Restful .

 api.add_resource(Records, '/rest/records/<string:email>/<string:password>/<string:last_sync_date>') parser = reqparse.RequestParser() parser.add_argument('record_date', type=str) parser.add_argument('records', type=str) parser.add_argument('rating', type=str) parser.add_argument('notes', type=str) class Records(Resource): def post(self, email, password, last_sync_date): args = parser.parse_args() records = args['records'] # 'records' = None, but why? return records, 201 

Unit test:

 resource_fields = { 'record_date': fields.String, 'rating': fields.Integer, 'notes': fields.String, 'last_updated': fields.DateTime, } records = {"records":[]} records["records"].append(marshal(record1, resource_fields)) rv = self.app.post('/rest/records/{0}/{1}/{2}'.format(email, password, sync_date), data=json.dumps(records)) 

json.dumps (entries):

 str: {"records": [{"rating": 1, "notes": null, "last_updated": "Tue, 15 Oct 2013 15:52:44 -0000", "record_date": "2013-10-15 15:52:44.746815"}]} 

Why args['records'] No, where do I explicitly send it by wire?

UPDATE:

The strange part is when I send one object, all its dandies. So strange:

 record = dict(record_date=record1.record_date, rating=record1.rating, notes=record1.notes, last_updated=record1.last_updated) rv = self.app.post('/rest/records/{0}/{1}/{2}'.format(email, password, sync_date), data=record) 

arg

 {'records': None, 'notes': None, 'task': None, 'record_date': '2013-10-15 16:48:40.662744', 'rating': '1'} 
+7
json python rest flask flask-restful
source share
2 answers

I ended up raising this as a problem with the flag-resful github and got this solution that works for me. Credit goes to Doug Black.

reqparse does not know how to handle JSON. To deal with JSON messages, you will want to use the flask.request.json dict.

Here is an updated example of what you probably want:

 from flask import Flask, request from flask.ext import restful class Records(restful.Resource): def post(self, email, password, last_sync_date): records = request.json['records'] return records, 201 app = Flask(__name__) api = restful.Api(app) api.add_resource( Records, '/rest/records/<string:email>/<string:password>/<string:last_sync_date>' ) 

The documents at request.json are here.

You will need to make sure that you publish the content type header set for the / json application, so the checkbox knows to populate the json dictionary.

 self.app.post( '/rest/records/{0}/{1}/{2}'.format(email, password, sync_date), data=json.dumps(records), headers={'Content-Type': 'application/json' ) 
+8
source share
 class Records(Resource): parser = reqparse.RequestParser() parser.add_argument('record_date', type=str) parser.add_argument('records', type=str) parser.add_argument('rating', type=str) parser.add_argument('notes', type=str) def post(self): args = parser.parse_args() records = args['records'] return records, 201 

I hope this works.

-2
source share

All Articles