Python Flask API: 在服务器上找不到请求的 URL。如果您手动输入 URL 请检查拼写并重试

Python Flask API : The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again

from flask import Flask, jsonify, request

app = Flask(__name__)
tasks = [
    {
        "id":1,
        "title":"You buy groceries",
        "description":"Milk, Cheeses, Pizza",
        "done":False,
    },
    {
        "id":2,
        "title":"Learn Python",
        "description":"The most important language in the world",
        "done":False,
    },
]

@app.route('/add-data', methods = ['POST'])

def add_data():
    if not request.json:
        return jsonify({
            "status":"error",
            "message":"Please provide the data"
        },400)
    task = {        
        "id":tasks[-1][id]+1,
        "title":request.json["title"],
        "description":request.json.get("description",""),
        "done":False,
    }
    tasks.append(task)
    return jsonify({
        "status":"success",
        "message":"Task added successfully!"
    })

@app.route("/get-data")

def get_task():
    return jsonify({
        "data":tasks
    })

if __name__ == "__main__":
    app.run(debug=True)

我只是这个 api 使用 python 创建的初学者,我试图创建一个 api 来获取数据和 post 数据。

但是当我 运行 代码时,它给我错误:

The screenshot of the error

如果代码中有错误或虚拟环境有问题,请告诉我。如果也能给出解决方案,将不胜感激

尝试“http://127.0.0.1:5000/get-data”

  • 它非常适合您的数据。

您还没有到 get 方法的自动路由。