4

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.':

(parameters using raw) enter image description here

enter image description here

(parameters using form-data) enter image description here

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!

2
  • What params are you sending with your requests? Commented Dec 28, 2015 at 15:35
  • as the screen shot above, I use {"name": "test", "slogan": "test"} as the raw Commented Dec 28, 2015 at 15:56

3 Answers 3

1

Just change your location="json" in to location="form" on lines: parser.add_argument(... location="json"). I fixxed by that.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you checked what's inside your request? You might get the values from the form-data using these:

request.form.get('name')
request.form.get('slogan')

Comments

0

In your second example when using reqparse, you're specifying the location of the form-data as location="json". The form-data is ignored if you do this because reqparse will only search the non-existing json body for that argument and immediately throw an error because it is required.

Using reqparse is probably the right way to go, although imo working with form-data is painful in general.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.