Using HTTP Methods - CRUD
00:00 In the previous lesson, I gave you a quick introduction to Pydantic. In this lesson, I’ll go over HTTP methods and how you use them in your API.
00:09 My inner 12-year-old child loves CRUD, the acronym for the four actions you need when dealing with data. You need to be able to create new objects, read the contents of objects, update objects, as in editing them, and delete objects.
00:25 Sometimes you can get away with not doing this one particular one if you’re a large social media company. Your web browser talks to a web server using a protocol known as HTTP.
00:36 This protocol supports a series of methods, which are the actions the browser performs against the server. For example, when you visit a webpage, that uses the HTTP GET method.
00:47 When you have a page with a form, the browser needs to send your data up to the server. It does that using HTTP POST. REST is a loosely defined mechanism for using the web to do data actions fast.
01:01 APIs use it. REST uses HTTP methods to determine what to do with an object. This is the verb, the action to perform, while the URL itself is what to perform it on.
01:13
The thing, the noun. The FastAPI decorators are named after these HTTP methods. You already saw the .get() decorator, which corresponds to the HTTP GET method.
01:26 Let’s go over the most common HTTP methods and how they are used in REST. First is GET, which is for reading, and it’s used in two ways. The first case is when you get a single item from the server.
01:39 To do that, the URL usually needs to include something that indicates what that item is. That’s typically an ID. Later on, our library API will support a call for fetching a specific book.
01:51
It will be book/{id}, for example, book/3, to fetch the book with ID 3. The other way GET is used in REST is to fetch a list.
02:01 This needs a different URL from the single item case and doesn’t contain a value. It does sometimes use URL query parameters to limit how big the list is, though.
02:11 There’s a whole lesson on that, so I’ll cover it more later. In our app, this kind of GET will be used to fetch a list of books.
02:20
HTTP POST is used to create objects, and the body of the HTTP message needs to contain JSON that describes the thing being created. For our book, that will mean the title, author, and pages attributes.
02:34 When you use POST to create an object, typically that new object gets a unique ID, which can then be used when fetching it back.
02:42 HTTP PUT is used to replace the contents of an object. Like with our GET case, it needs an ID of something to replace. Sometimes that’s in the URL, and sometimes it’s in the HTTP message body.
02:56 Like with POST, the message body has the new contents for the object. As it’s a replace, it should overwrite the thing with the same ID. And finally, HTTP DELETE is for painting pretty pictures in the sky with a small airplane, or more likely, it does exactly what it says.
03:13 Like with our fetch, its URL includes the ID of the thing to be deleted. There are more HTTP methods out there, but these are the core ones used for CRUD.
03:23 Your API doesn’t have to implement them all. For example, you might only provide read capabilities for your data, leaving out the CUD part of CRUD.
03:33 Alright, now you’ve seen how REST uses HTTP methods to manage your data. And in previous lessons you saw data validation with Pydantic and FastAPI decorators for your functions.
03:43 So you’re all set to write an app. In the next lesson, I’ll show you how to fetch a book from our digital library API.
Become a Member to join the conversation.
