I am using Python Flask-Restful to make a post request. And I use PostMan (Chrome) to test my apis. I set the ContentType to application/json in the header part of postman. And I can get the parameters only in the form of raw value, when I change to form-data, I got the error message of 'The browser (or proxy) sent a request that this server could not understand.':
Here is my code:
# -*- coding: UTF-8 -*-
from app import app, db, models, api, DataModels
from flask.ext import restful
from flask.ext.restful import reqparse
from flask import jsonify, request
class SchoolListHandler(restful.Resource):
def post(self):
json_data = request.get_json(force=True)
name = json_data['name']
slogan = json_data['slogan']
print "name is: %s, slogan is: %s" % (name, slogan)
return jsonify(result="xxxx")
api.add_resource(SchoolListHandler, "/api/allSchools")
Also, I have tried to use reqparse to get my parameters, but the problem is still not solved:
# -*- coding: UTF-8 -*-
from app import app, db, models, api, DataModels
from flask.ext import restful
from flask.ext.restful import reqparse
from flask import jsonify, request
class SchoolListHandler(restful.Resource):
def get(self):
all_schools = DataModels.School.School.query.all()
return jsonify(data=[x.json for x in all_schools])
def post(self):
parser = reqparse.RequestParser()
parser.add_argument("name", type=unicode, required=True, location="json")
parser.add_argument("slogan", type=unicode, required=True, location="json")
args = parser.parse_args()
name = args['name']
slogan = args['slogan']
return jsonify(result="xxxx")
api.add_resource(SchoolListHandler, "/api/allSchools")
So, how can I work this out, thanks in advance!


