Home > Mobile >  Error code 304 in flask python with GET method
Error code 304 in flask python with GET method

Time:01-12

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

@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

CodePudding user response:

@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"
  • Related