#277 How it works, by example 6.6 min
How REST APIs Work: Build One with the Standard Library
9 calls to a tiny notes API gave 6 status codes, from 200 to 405.
Predict and run the nine calls
Copy the code folder first. Read the list of 9 calls in run.py and write down the status code you expect for each one. Then run python run.py and compare. Which of your guesses were wrong, and why?
A hint
Look at the notes dictionary in api.py to see which ids exist before the POST.
Break the PUT on purpose
In run.py, change the PUT call so its body has only a text and no done field. Predict the status code and the error message, then run it. You should see 400 Bad Request with the message missing field: done. Now explain why the PATCH call that follows still works.
A hint
PUT needs every field because it replaces the whole note. PATCH changes only what you send.
Send a wrong type
Add a PATCH call to run.py on /notes/1 with the body {"done": "yes"}. Predict the answer, then run it. You should see 400 Bad Request with the message bad field: done. Find the line in api.py that checks the type of each value.
A hint
Look at the invalid function and the FIELDS dictionary.
See the ids get reused
At the end of the CALLS list in run.py, add a GET on /notes and then a POST with the text "x". Predict the id of the new note and the Location header, then run it. After the earlier DELETE of note 3, you should see Location: /notes/3 again. The video says ids can be reused after a delete. Here the newest note was deleted, so its id came back. Find the line in api.py that explains why. Deleting an older note, such as 1, would not give its id back.
A hint
The new id is the largest existing id plus 1.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
The notes and the server are a made-up toy for teaching. HEAD and OPTIONS get 501, and there is no authentication.
