0

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', "")

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.