Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item PUT HTTP response - correction proposal #59

Closed
csabaszeles opened this issue Jul 26, 2022 · 1 comment
Closed

Item PUT HTTP response - correction proposal #59

csabaszeles opened this issue Jul 26, 2022 · 1 comment

Comments

@csabaszeles
Copy link

def put(self, name):
data = Item.parser.parse_args()
item = ItemModel.find_by_name(name)
if item:
item.price = data['price']
else:
item = ItemModel(name, **data)
item.save_to_db()
return item.json()

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

@jslvtr
Copy link
Contributor

jslvtr commented Sep 24, 2022

Thank you for this! Since we have updated the entire course content, I'll close this for now.

@jslvtr jslvtr closed this as completed Sep 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants