rest-apis-flask-python icon indicating copy to clipboard operation
rest-apis-flask-python copied to clipboard

Item PUT HTTP response - correction proposal

Open csabaszeles opened this issue 2 years ago • 0 comments

https://github.com/tecladocode/rest-apis-flask-python/blob/3380640669b90e907f65660225af2f06d6b883f2/section6/resources/item.py#L48-L60

Hi, In Lec99, the HTTP response of PUT is not fully correctly dealt with. According to this: https://httpwg.org/specs/rfc7231.html, PUT should return 201, when new resource has been created. (Also, added error handling similarly to the post() method) So, I think implementation of put() should be appended with the HTTP response code handling, like this:

    def put(self, name):
        data = Item.parser.parse_args()
        item = ItemModel.find_by_name(name)

        # if item with name doesn't exist, we create it
        if item is None:
            item = ItemModel(name, **data)
            http_status_code = 200
        else:
            item.price = data["price"]
            http_status_code = 201

        try:
            item.save_to_db()
        except:
            return {"message": "An error occurred upserting the item."}, 500

        return item.json(), http_status_code

Best, Csb

csabaszeles avatar Jul 26 '22 13:07 csabaszeles