EyeOfTheOwl
EyeOfTheOwl

Reputation: 57

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!

Upvotes: 3

Views: 1437

Answers (1)

Xema
Xema

Reputation: 1886

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

Upvotes: 5

Related Questions