Modern APIs with FastAPI and Python Transcripts
Chapter: Building our first API
Lecture: Know your HTTP verbs
0:00 You can see that working with http verbs: get, post, put, and so on,
0:03 all of those things are right up in front when we work with building
0:08 API's with FastAPI,
0:10 and understanding what verbs are out there and when they should be used is really important
0:14 when you're building REST-ful API's.
0:17 So let's just take a moment and talk about the four most common http verbs.
0:21 There are others that we're not gonna mention here, you can go Look at this link at
0:25 the bottom to see them all.
0:26 The one that everyone who has interacted with a computer on the web is familiar with
0:31 is "get". So get is just give me the information of this resource.
0:35 Like, anytime you're using a browser,
0:36 most things you're doing are just click a link,
0:39 do it get. so the get requests a representation of a specified resource, in our case
0:44 the result of that calculation, and it should only be used to retrieve data.
0:50 One of the really important ideas around get is that it's supposed to be potentially cacheable,
0:55 whether or not you access it multiple times or whatever, it shouldn't change the state of
0:59 the system. There was a really interesting bug in Wikipedia way,
1:04 way, way back in the early days when there was a delete button,
1:07 actually had a just a get request to delete a resource,
1:10 and then the web spiders like Google went through and started trying to index Wikipedia and
1:14 started following those links and started deleting the pages.
1:17 So, yeah, don't do that kind of stuff.
1:19 Get should just return things. Think of get as "read only".
1:23 If you wanna change something, if you want to modify something,
1:26 use "http post". This is to submit some new object,
1:31 some new data, which is going to cause a change in the server, potentially.
1:36 So if you're gonna log in,
1:37 you're gonna create an account, you might submit your username and your password that
1:41 you want to create the account with.
1:43 And of course, the state that's being changed on the server as well
1:46 now there's a new account with that username and password that you can log in.
1:50 Delete, similar to post in that it modifies things,
1:53 but if I want to remove a resource, so if I had like a bookstore and
1:57 I had "/books/book_7"
1:59 and we decided, you know,
2:00 we don't want book seven anymore, then the
2:03 API could set it up so I do a delete request against that
2:06 URL, against "books/book_7" and the server could respond by deleting book seven, no
2:12 longer there. Also similar to post is we have put. Now with post you say
2:17 "I have some data, I want you to create it on the server".
2:19 Probably what comes back is "Great,
2:22 I created this, and here is where it lives".
2:25 But if you already know where you want it to be in terms of the
2:29 URL, think of like a blog post, a blog post
2:31 you might already know the url where you want your blog post to live,
2:34 but it doesn't yet exist, you could do an http
2:37 put and say "I would like to make this location have this blog post",
2:41 right? You don't let the server control where the thing gets created,
2:45 you say explicitly "server Put it here".
2:47 This is not nearly as common as post,
2:49 but sometimes you'll see it used. These are
2:52 the four most common http verbs as far as writing API's and you know
2:57 explicitly making them part of your API.
2:59 There are a bunch of others,
3:00 like I said, at the bottom,
3:01 you can check out, but get for read only, post for making changes,
3:05 delete to delete, put if you know where it's going.