Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 296
Ohad Perry Avatar asked Oct 28 '22 23:10

Ohad Perry


1 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.

like image 65
davidism Avatar answered Nov 15 '22 06:11

davidism