'Trying to get data from a list of dictionaries into a route [duplicate]
So I have set up a flask API with the following data
characters = [
{ "id": 1, "Name": "Kyle", "Location": "London"},
{ "id": 2, "Name": "Peter", "Location": "New York"},
{ "id": 3, "Name": "Venti", "Location": "London"},
{ "id": 4, "Name": "Katya", "Location": "Paris"},
]
And I have a route /characters that gives me all of the information using the functions
def index(req):
return [character for character in characters], 200
@app.route('/characters', methods = ['GET'])
def users_handlers():
fns = {
'GET': characters.index,
}
resp, code = fns[request.method](request)
return jsonify(resp), code
Is there a way to make a route similar to /characters/<Location> which would give me an output of the following if the route is /characters/London
{
"Location": "London",
"Name": "Kyle",
"id": 1
},
{
"Location": "London",
"Name": "Venti"
"id": 3
}
Solution 1:[1]
Yes. There is a way
@app.route('/characters/<location> ')
def get_location(Location):
return "The location is " + str(location)
Here you can find more examples.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user2976612 |
