I ran into a similar problem, and here is a solution that works for me. let's say your application looks like this:
from flask import Flask, jsonify from flask_restful import Api, Resource, reqparse app = Flask(__name__) api = Api(app) # Define parser and request args parser = reqparse.RequestParser() parser.add_argument('last_name', type=str) parser.add_argument('first_name', type=str) # not the type=dict parser.add_argument('personal_data', type=dict) class Item(Resource): def post(self): args = parser.parse_args() ln = args['last_name'] fn = args['first_name'] # we can also easily parse nested structures age = args['personal_data']['age'] nn = args['personal_data']['nicknames'] return jsonify(fn=fn, ln=ln, age=age, nn=nn) api.add_resource(Item, '/item') if __name__ == '__main__': app.run(debug=True)
Now you can easily create some JSON data:
import json d = {'last_name': 'smith', 'first_name': 'john', 'personal_data': {'age': 18, 'height': 180, 'nicknames': ['johnny', 'grandmaster']}} print(json.dumps(d, indent=4)) { "last_name": "smith", "first_name": "john", "personal_data": { "age": 18, "height": 180, "nicknames": [ "johnny", "grandmaster" ] } } json.dumps(d) '{"last_name": "smith", "first_name": "john", "personal_data": {"age": 18, "height": 180, "nicknames": ["johnny", "grandmaster"]}}'
and call the application:
curl http://localhost:5000/item -d '{"last_name": "smith", "first_name": "john", "personal_data": {"age": 18, "height": 180, "nicknames": ["johnny", "grandmaster"]}}'
This will fail (I cut the trace):
age = args ['personal_data'] ['age']
TypeError: 'NoneType' object cannot be signed
the reason is that the title is not specified. If we add
-H "Content-Type: application/json"
and then call
curl http://localhost:5000/item -H "Content-Type: application/json" -d '{"last_name": "smith", "first_name": "john", "personal_data": {"age": 18, "height": 180, "nicknames": ["johnny", "grandmaster"]}}'
The output looks as expected:
{ "age": 18, "fn": "john", "ln": "smith", "nn": [ "johnny", "grandmaster" ] }
This function can also be further simplified to:
class Item(Resource): def post(self): json_data = request.get_json()
as shown above .