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)
I am just a beginner in this api creating with python and I was trying to create an api to get the data and to post data.
But when I run the code, it gives me the error:
Let me know if there is some error in the code or problem of the virtual environment. Would be very much grateful if the solution is also given.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.