Modern APIs with FastAPI and Python Transcripts
Chapter: Building our first API
Lecture: Know your HTTP status codes
0:00 http verbs are what the client is instructing the server to do and telling what the
0:05 server to do. But the server has an ability to talk back to the clients say "yes, that worked" or "no, that wasn't okay because of this",
0:13 or "you don't have permission". So the way the server communicates that is with http status codes.
0:17 So if you are not super up on your http status codes, you're gonna need to really get those a few of those figured out to
0:24 see exactly what you should tell all of your clients. So a really good place to go is "httpstatuses.com" statuses not status.
0:32 There we go, http status codes. Now, This is a really cool site because it doesn't just have them grouped and categorized, but it has details.
0:40 So before you saw we got a 200 OK when we did a request for our calculate. If you click on this, you get all sorts of information about when you should
0:49 do that, how you should do it okay, so get represents, it's a good thing to return for a get If
0:54 here's a proper representation. The payload sends a 200 response. It depends on the request method.
1:00 The meaning is basically "that worked" or this is the same as get but no data and so on. Now, if you come down here, you can even see, like,
1:08 some of the places you might use it. There's like constants in Python, like "http.HTTPstatus.OK" And you can use those if you like.
1:16 You can also come down here and we could see created. This is much more common for a http post. You know, once you've done a post like hey,
1:23 that worked, I created it, And then you're going to say where it was. So all the 200's, these are
1:28 good. Over in the 400 section is another place where you definitely wanna look. So bad
1:32 request. Bad request is awesome because it means you passed some data, like invalid data, or some other thing that's not good,
1:40 so we're not going to process that. We come down here and say "you don't have permission to do it", or "this data was unacceptable",
1:48 so on. And then finally, the ones we hope we don't see too much of are the 500 internal server errors.
1:54 So this is like the server crashed or some part of our infrastructure couldn't get to another part, NGINX Couldn't talk to Gunicorn,
2:02 one of those types of things. Definitely look through here when you're thinking about what should I send back
2:07 to answer this request, especially if there's something that went wrong. What should I send back? "httpstatuses" is a great place to go Look.