One of the fields in our caf database is the price of a single black coffee. It's a good way for users to gauge how expensive is the coffee shop. But cafes often change their prices. What if a user wanted to submit a change in price at one of the cafes?
If they knew the id
of the caf (which they can get by making a GET request to fetch data on all the cafes), then they can update the coffee_price
field of the caf.
In this situation, a PATCH
request is probably more efficient, as we don't need to change any of the rest of the cafe's data.
1. Create a PATCH
request route in main.py to handle PATCH requests to our API. In order for our API to be RESTful, ideally, the route should be something like this:
/update-price/<cafe_id>
So the user might go to localhost:5000/update-price/22
and that would update the caf with an id of 22.
HINT 1: You can use .get_or_404()
easily get a caf by a particular id.
HINT 2: The user will also need to provide the updated price of a single black coffee by passing it with the request as a parameter.
Change the request type inside Postman to PATCH. This is what should happen if you've done this correctly, you should be able to test the API in Postman and get a successful response:
NOTE: There might be a chance that the id in the route doesn't exist. In this case, make sure you give the user the correct feedback:
NOTE: Notice that even when the resource is not found and we get an error the correct HTTP code is not being returned. It should be 404 for "resource not found" but instead we're getting 200 for "a ok".
This is how you can pass an HTTP code with your response: