JLam
JLam

Reputation: 11

Python 3 Flask Rest API Get multiple argument

How to have get rest api on python with multiple arguments? I tested my rest api on IE by the below url

http://127.0.0.1:5002/search_np?item=testing&posn=1

from flask import Flask, request
from flask_restful import Resource, Api
from flask_cors import CORS

....

app = Flask(__name__)
cors = CORS(app, resources={r"*": {"origins": "*"}})
api = Api(app)
api.add_resource(search_nextpage, '/search_np')

....
class search_nextpage(Resource):
    def get(self):
        search_item = request.args.get('item', "")
        search_posn =request.args.get('posn', "")

Upvotes: 0

Views: 2169

Answers (1)

Osvald Laurits
Osvald Laurits

Reputation: 1364

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

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


class search_nextpage(Resource):
    def get(self):
        item = request.args.get('item')
        posn = request.args.get('posn')
        return {'item': item, 'posn': posn}


api.add_resource(search_nextpage, '/search_np')

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

Requesting http://127.0.0.1:5000/search_np?item=chocolate&posn=0 yields the following output.

{
    "item": "chocolate",
    "posn": "0"
}

The arguments item and posn are retrieved from the querystring and returned in a json dictionary.

Upvotes: 6

Related Questions