Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Restful - Status 400 when using parser_args() to add arguments to requests in python

I am trying to use flask_restful to create an API using Python. I have the following code:

from flask import Flask
from flask_restful import Resource, Api, reqparse

app = Flask(__name__)
api = Api(app)

class Example(Resource):

    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('some_arg')
        args = parser.parse_args()
        return {"Param Entered": args['some_arg']}

api.add_resource(Example, '/')

if __name__ == '__main__':
    app.run()

When running the GET request in Postman:

GET <my_url>/?some_arg=<some_text>

I get the error: "message": "The browser (or proxy) sent a request that this server could not understand."

Could someone please explain why this error shows and how I can add arguments properly using parse_args() for flask restful? Thank you!

like image 273
EyeOfTheOwl Avatar asked Nov 28 '25 04:11

EyeOfTheOwl


1 Answers

A bit late to the party, but I also got the issue and managed to solve it by chaning parser.add_argument('some_arg') to parser.add_argument('some_arg', location='args').

Without the location parameter, it was working fine on my Windows machine, but not on my Linux one (EC2 instance).

like image 188
Xema Avatar answered Nov 29 '25 17:11

Xema



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!