'How do I make this Flask API return a value? [duplicate]
I have a Python Flask script with a JSON that I have developed. I am trying to return the cities using this url: http://127.0.0.1:5000/restaurant/city.
I get an this error:
Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Why is my logic error in my code?
I am using Python 3.8 and Flask.
Here is my code (the JSON is in the code):
from flask import Flask, jsonify, request
import json
# from urllib3 import request
# import urllib.request as urllib
app = Flask(__name__)
restaurant = [
{"city": "Al-Khobar",
"details": [
{
"id": 1,
"Area": "Near Corniche",
"Restaurant": "Naranj Damascus Restaurant",
"Street": "Firas Bin Al Nadr Street",
"location link": "https:\\/\\/www.tripadvisor.com\\/Restaurant_Review-g298545-d22895043-Reviews-Naranj_Damascus_Restaurant-Al_Khobar_Eastern_Province.html#MAPVIEW",
"City": "Al Khobar",
"Zip": 34447,
"Country": "Saudi Arabia",
"phone": 966508446622,
"meals": "Breakfast, Lunch, Dinner, Late Night",
"cuisine": "International, Barbecue, Grill, Diner, Middle Eastern",
"price range": "'$25 to $50"
},
{
"id": 2,
"Area": "Near Corniche",
"Restaurant": "The Butcher Shop and Grill",
"Street": "Prince Turkey Street",
"location link": "https:\/\/www.tripadvisor.com\/Restaurant_Review-g298545-d10691837-Reviews-The_Butcher_Shop_and_Grill-Al_Khobar_Eastern_Province.html#MAPVIEW",
"City": "Al Khobar",
"Zip": " ",
"Country": "Saudi Arabia",
"phone": 966138085182,
"meals": "Lunch, Dinner",
"cuisine": "Steakhouse",
"price range": "'$25 to $50"
},
{
"id": 3,
"Area": "Near Corniche",
"Restaurant": "Kosebasi, Al Khobar",
"Street": "Prince Turkey Street",
"location link": "https:\/\/www.tripadvisor.com\/Restaurant_Review-g298545-d9874670-Reviews-Kosebasi_Al_Khobar-Al_Khobar_Eastern_Province.html#MAPVIEW",
"City": "Al Khobar",
"Zip": " ",
"Country": "Saudi Arabia",
"phone": 966138030089,
"meals": "Lunch, Dinner",
"cuisine": "Turkish, Middle Eastern, Barbecue",
"price range": "'$25 to $50"
},
{
"id": 4,
"Area": "Near Corniche",
"Restaurant": "Bun & Patty",
"Street": "Prince Turkey Street Al Yarmouk",
"location link": "https:\/\/www.tripadvisor.com\/Restaurant_Review-g298545-d8054714-Reviews-Bun_Patty-Al_Khobar_Eastern_Province.html#MAPVIEW",
"City": "Al Khobar",
"Zip": 344233213,
"Country": "Saudi Arabia",
"phone": " ",
"meals": "Lunch, Dinner",
"cuisine": "American, Fast Food",
"price range": " "
}
]
},
{"city": "Dammam",
"details": [
{
"id": 5,
"Area": "Near Corniche",
"Restaurant": "Abu Nawas",
"Street": "Al Adama – Prince Mansour Street",
"location link": "https://www.tripadvisor.com/Restaurant_Review-g298547-d805085-Reviews-Abu_Nawas-Dammam_Eastern_Province.html#MAPVIEW",
"City": "Dammam",
"Zip": "31461",
"Country": "Saudi Arabia",
"phone": 966138266363,
"meals": " ",
"cuisine": "Lebanese, Mediterranean, Middle Eastern",
"price range": "'$5 to $40"
},
{
"id": 6,
"Area": "Near Corniche",
"Restaurant": "Heritage Village",
"Street": "Prince Turkey Street",
"location link": "https://www.tripadvisor.com/Restaurant_Review-g298547-d805123-Reviews-Heritage_Village-Dammam_Eastern_Province.html#MAPVIEW",
"City": "Dammam",
"Zip": " ",
"Country": "Saudi Arabia",
"phone": 96638090000,
"meals": "Lunch, Dinner",
"cuisine": "Middle Eastern, Vegetarian Friendly, Halal",
"price range": "'$10 to $30"
},
{
"id": 7,
"Area": "Near Corniche",
"Restaurant": "Manoosha Alreef",
"Street": "Prince Faisal Bin Fahad Road Khobar North",
"location link": "https://www.tripadvisor.com/Restaurant_Review-g298547-d10221865-Reviews-Manoosha_Alreef-Dammam_Eastern_Province.html#MAPVIEW",
"City": "Dammam",
"Zip": "34426",
"Country": "Saudi Arabia",
"phone": 966539222673,
"meals": "Breakfast, Lunch, Dinner",
"cuisine": "Bakeries, Lebanese, Fast Food",
"price range": "'$25 to $50"
},
{
"id": 8,
"Area": "Near Corniche",
"Restaurant": "American, Steakhouse",
"Street": "Prince Mohammad Bin Fahad St.",
"location link": "https://www.tripadvisor.com/Restaurant_Review-g298547-d2659493-Reviews-Steak_House-Dammam_Eastern_Province.html#MAPVIEW",
"City": "Dammam",
"Zip": 11372,
"Country": "Saudi Arabia",
"phone": "96638335468",
"meals": "Lunch, Dinner",
"cuisine": "American, Fast Food",
"price range": "$10 to $35"
}
]
}
]
@app.route('/restaurant')
def hello():
return jsonify(restaurant)
@app.route('/restaurant/city')
def places():
for x in jsonify(restaurant):
if x[jsonify(restaurant)] == 'city':
return x
return {'city': None}
# return jsonify(restaurant['city'])
@app.route('/restaurant', methods=['POST'])
def add_subscription():
subscription = request.get_json(force=True)
restaurant.append(subscription)
return {'id': len(restaurant)}, 200
@app.route('/restaurant/<int:index>', methods=['PUT'])
def update_subscription(index):
subscription = request.get_json(force=True)
restaurant[index] = subscription
return jsonify(restaurant[index]), 200
@app.route('/restaurant/<int:index>', methods=['DELETE'])
def delete_subscription(index):
restaurant.pop(index)
return 'None', 200
app.run()
Solution 1:[1]
A few issues with the code.
- the
restaurantvariable you provided doesn't need to be "jsonified", you can use it as is. - the path
/restaurant/cityis supposed to have thecityas a variable from the user, to achieve that you need to declare the decorator as below. Also now the "city" is argument to the method too:
@app.route('/restaurant/<city>')
def places(city):
And then I guess you are trying to match the variable city with the city from the dictionaries, so the whole code would become:
@app.route('/restaurant/<city>')
def places(city):
for x in restaurant:
if x["city"] == city:
return x
return {'city': None}
To test the code, you should use a URL like: http://127.0.0.1:5000/restaurant/Dammam
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 | halfer |
