代码之家  ›  专栏  ›  技术社区  ›  ironmantis7x

如何让这个API返回一个值

  •  1
  • ironmantis7x  · 技术社区  · 3 年前

    我有一个python flask脚本,其中包含我开发的JSON。 我正在尝试使用以下url返回城市: http://127.0.0.1:5000/restaurant/city .

    我发现了一个错误:

    内部服务器错误服务器遇到内部错误 无法完成您的请求。要么服务器超载,要么 应用程序中有一个错误。

    我不确定代码中的逻辑错误在哪里。 有人能帮我吗?

    我正在使用python 3.8和flask。

    以下是我的代码(代码中包含json):

    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()
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   ilias-sp    3 年前

    代码有一些问题。

    1. 这个 restaurant 您提供的变量不需要“jsonified”,您可以按原样使用它。
    2. 道路 /restaurant/city 应该有 city 作为来自用户的变量,要实现这一点,需要如下声明decorator。现在,“城市”也是该方法的论据:
    @app.route('/restaurant/<city>')
    def places(city):
    

    然后我猜你在尝试匹配变量 城市 随着字典中的城市,整个代码将变成:

    @app.route('/restaurant/<city>')
    def places(city):
        for x in restaurant:
            if x["city"] == city:
                return x
        return {'city': None}
    

    要测试代码,应该使用如下URL: http://127.0.0.1:5000/restaurant/Dammam