#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: FastAPI
Lecture: Know your HTTP verbs

Login or purchase this course to watch this video and the rest of the course contents.
0:00 You can see that working with HTTP verbs, get, post, put, and so on, all of those things
0:05 are right up in front when we work with building APIs with FastAPI.
0:11 And understanding what verbs are out there and when they should be used is really important when you're building RESTful APIs.
0:18 So let's just take a moment and talk about the four most common HTTP verbs. There are others that we're not going to mention here.
0:25 You can go look at this link at the bottom to see them all. The one that everyone who has interacted with a computer on the web is familiar with is
0:32 get. So get is just give me the information on this resource. Like any time you're using a browser, most things you're doing are just click a link,
0:40 do a get. So the get requests a representation of a specified resource, in our case the result
0:46 of that calculation, and it should only be used to retrieve data.
0:51 One of the really important ideas around get is that it's supposed to be potentially cacheable,
0:56 whether or not you access it multiple times or whatever. It shouldn't change the state of the system.
1:02 There was a really interesting bug in Wikipedia way, way, way back in the early days when
1:06 there was a delete button, actually had a just a get request to delete a resource.
1:11 And then the web spiders like Google went through and started trying to index Wikipedia
1:15 and started following those links and started deleting the pages. So yeah, don't do that kind of stuff. Get to just return things.
1:22 Think of get as read only. If you want to change something, if you want to modify something, use HTTP post.
1:29 This is to submit some new object, some new data, which is going to cause a change in the server potentially.
1:36 So if you're going to log in, you're going to create an account, you might submit your
1:41 username and your password that you want to create the account with. And of course, the state that's being changed on the server as well.
1:47 Now there's a new account with that username and password that you can log in.
1:51 Delete similar to post in that it modifies things, but if I want to remove a resource,
1:57 so if I had like a bookstore and I had slash books slash book seven, and we decide, you know, we don't want book seven anymore.
2:03 And the API could set it up. So I do a delete request against that URL against book slash seven, and the server could
2:11 respond by deleting book seven, no longer there. Also similar to post is we have put now with post you say I have some data, I want you
2:19 to create it on the server. Basically what comes back is great, I recreated this and here is where it lives.
2:26 But if you already know where you want it to be in terms of the URL, think of like a
2:30 blog post, blog posts, you might already know the URL where you want your blog post to live,
2:35 but it doesn't yet exist, you could do an HTTP put and say, I would like to make this location have this blog post, right?
2:43 You don't let the server control where the thing gets created. You you say explicitly server put it here.
2:49 This is not nearly as common as post but sometimes you'll see it used in these are the four most
2:54 common HTTP verbs as far as writing APIs and you know, explicitly making them part of your API.
3:01 There are a bunch of others like I said at the bottom you can check out but get for read
3:04 only posts for making changes, delete to delete put if you know where it's going.


Talk Python's Mastodon Michael Kennedy's Mastodon