flask request args parser error The browser (or proxy) sent a request that this server could not understand

Question:

using test_client and sending a request like so:

app = Flask(__name__)
client = app.test_client()

headers = {'content-type': 'application/json', 'Authorization': u'Bearer fake_token_123'}
params = {'dont_care': True}
client.get(ֿֿ'/my_route/123', query_string=params, headers=headers)

my route is

class MyController(Resource):

    def get(self, id):
        parser = reqparse.RequestParser()
        parser.add_argument('currency', type=str, default='USD', help="help text")

        args = parser.parse_args()
        currency_from_params = args.currency

parser.parse_args() failing for

The browser (or proxy) sent a request that this server could not understand

when removing 'content-type': 'application/json' from header it works.

I don’t understand this behaviour, and how do I protect against it without the un elegant try, expect.

Thanks for your help

Asked By: Ohad Perry

||

Answers:

You already discovered how to fix it: don’t send content-type: application/json if you’re not posting JSON. You can’t send JSON with GET, and even if you could (or were using POST) you would have to encode the JSON with json.dumps(data) first.

Answered By: davidism

This error also appears if you put your key value pairs in a POST into the request parameters instead of creating a proper JSON.

Answered By: Greg Holst
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.