Modern APIs with FastAPI and Python Transcripts
Chapter: Building our first API
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 are right up in front when we work with building
0:09 API's with FastAPI, and understanding what verbs are out there and when they should be used is really important when you're building REST-ful API's.
0:18 So let's just take a moment and talk about the four most common http verbs.
0:22 There are others that we're not gonna mention here, you can go Look at this link at the bottom to see them all.
0:27 The one that everyone who has interacted with a computer on the web is familiar with is "get". So get is just give me the information of this resource.
0:36 Like, anytime you're using a browser, most things you're doing are just click a link,
0:40 do it get. so the get requests a representation of a specified resource, in our case
0:45 the result 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
1:00 the system. There was a really interesting bug in Wikipedia way, way, way back in the early days when there was a delete button,
1:08 actually had a just a get request to delete a resource, and then the web spiders like Google went through and started trying to index Wikipedia and
1:15 started following those links and started deleting the pages. So, yeah, don't do that kind of stuff.
1:20 Get should just return things. Think of get as "read only". If you wanna change something, if you want to modify something,
1:27 use "http post". This is to submit some new object, some new data, which is going to cause a change in the server, potentially.
1:37 So if you're gonna log in, you're gonna create an account, you might submit your username and your password that you want to create the account with.
1:44 And of course, the state that's being changed on the server as well 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, so if I had like a bookstore and I had "/books/book_7"
2:00 and we decided, you know, we don't want book seven anymore, then the API could set it up so I do a delete request against that
2:07 URL, against "books/book_7" and the server could respond by deleting book seven, no
2:13 longer there. Also similar to post is we have put. Now with post you say "I have some data, I want you to create it on the server".
2:20 Probably what comes back is "Great, I created this, and here is where it lives". But if you already know where you want it to be in terms of the
2:30 URL, think of like a blog post, a blog post 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",
2:42 right? You don't let the server control where the thing gets created, you say explicitly "server Put it here". This is not nearly as common as post,
2:50 but sometimes you'll see it used. These are the four most common http verbs as far as writing API's and you know
2:58 explicitly making them part of your API. There are a bunch of others, like I said, at the bottom,
3:02 you can check out, but get for read only, post for making changes, delete to delete, put if you know where it's going.


Talk Python's Mastodon Michael Kennedy's Mastodon