I am creating an API using Flask-RESTful, and am attempting to make a resource that returns all items in a database that are like a user defined string.
The endpoint for this resource is defined as:
api = Api(app)
api.add_resource(ItemsList, '/privateapi/item?query=<string:tag>')
And the resource itself is:
class ItemsList(Resource):
def get(self, tag):
search = f"%{tag}%"
items = ItemModel.query.filter(ItemModel.item_name.like(search)).all()
return {"items": [item.json() for item in items]}
The problem is that when I send a GET request to the endpoint http://127.0.0.1:5000/privateapi/item?query=appl I get a 404 response.
When I change the endpoint to api.add_resource(ItemsList, '/privateapi/item/<string:tag>') and then query the API I get the desired response.
However, I would prefer to use the query approach since I am not trying to GET a single record but want to return an array. I just think it makes more sense this way.
What could I try?