Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error code 304 in flask python with GET method

I’m new to python and I faced an error which I totally don’t understand why occures.
In the Insomnia client REST API I’m creating item with POST method, and it works well, below it the code

@app.post('/item')
def create_item():
    item_data = request.get_json()
    if (
        "price" not in item_data
        or "store_id" not in item_data
        or "name" not in item_data
    ):
        abort(
            400,
            message="Bad request"
        )

    for item in items.values():
        if (
            item_data["name"] == item["name"]
            and item_data["store_id"] == item["store_id"]
        ):
            abort(400, message="Item already exist")
    if item_data["store_id"] not in stores:
        abort(404, message="Store not found")

    if item_data["store_id"] not in stores:
        abort(404, message="Store not found")

    item_id = uuid.uuid4().hex
    item = {**item_data, "id": item_id}
    items["item_id"] = item

    return item, 201

and here is the outcome of post method, created item with "id"
{
"id": "1c0deba2c86542e3bde3bcdb5da8adf8",
"name": "chair",
"price": 17,
"store_id": "e0de0e2641d0479c9801a32444861e06"
}

when I run GET method using "id" from above item putting it to the link I get error code 304

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@app.get("/item/<string:item_id>")
def get_item(item_id):
    try:
        return items[item_id]
    except KeyError:
        abort(404, message="Item not found")

enter image description here

Can You please suggest what is wrong here ?

thanks

>Solution :

@app.route('/item/<string:id>')
def get_item(id):
    instance = YourMOdel.query.filter_by(id=id).first()
    if instance:
        return render_template('data.html', instance = instance)
    return f"Employee with id ={id} Doenst exist"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading