0
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 :

The screenshot of 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.

Akash Maity
  • 19
  • 1
  • 3

1 Answers1

1

try "http://127.0.0.1:5000/get-data"

  • it pretty much serves your data. enter image description here

you do not have an auto route to the get-method yet.

  • Thanks a lot, this actually worked! And also, please tell me how to do that auto route to get-method. – Akash Maity Apr 14 '21 at 09:57
  • A) On a basic page you can leave the "/get-data" a part away the webserver knows how to check the type of html request, as long as there' only one of each type... or: b) u can use redirection from the base url to the initial one (via html), c) there are acutally more smarter ways to accomplish this result. (I'm not a pro yet but stack overflow will have a good answer for that, too :) – Alexander Brockmeier Apr 14 '21 at 17:17
  • Actually auto route does sth. different than I thought, but it might be useful to understand this and the use of decorators anyway...: https://stackoverflow.com/questions/35015763/flask-auto-detect-changes-to-routes-in-production – Alexander Brockmeier Apr 14 '21 at 17:21