Faiz Hassan
Faiz Hassan

Reputation: 1

is there a way to pass parameter of flask api to my python code

hello am trying to create flask api where user input the vaue and I will use that value to process my python code here is the code

`

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

class Users(Resource):
    @app.route('/users/<string:name>/')
    def hello(name):
        namesource = request.args.get('name')
        return "Hello {}!".format(name)

print(namesource)  # here am trying to get the sitring/value in name source but i can't because it no       variable defines

api.add_resource(Users, name='users')
# For Running our Api on Localhost
if __name__ == '__main__':
    app.run(debug=True)

`

am trying to expect that i get that value/String outside of the function of api flask

Upvotes: 0

Views: 459

Answers (1)

Fastnlight
Fastnlight

Reputation: 1158

You are trying to access the variable namesource outside of the function hello. You can't do that. You can access the variable name outside of the function hello because it is a parameter of the function. You can fix it by making a global variable.

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

namesource = None

class Users(Resource):
    @app.route('/users/&lt;string:name&gt;/')
    def hello(name):
        global namesource
        namesource = request.args.get('name')
        return "Hello {}!".format(name)

print(namesource)

api.add_resource(Users, name='users')
# For Running our Api on Localhost
if __name__ == '__main__':
    app.run(debug=True)

Upvotes: 1

Related Questions