Modern APIs with FastAPI and Python Transcripts
Chapter: Accepting inbound data
Lecture: Playing nice with status codes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, when we did this call here, we did this post. We passed some data that was in the body. It created this entity over here and everything worked.
0:10 So what did FastAPI do? It said "great, everything worked. 200 ok". But let's drop over on our again
0:18 and see about these success codes. So Okay, well, that's kind of the most generic term. And here's a representation of the resource.
0:25 We actually did give that back, so it's kind of okay. But what would make more sense would be created. Over here, we're creating a new thing,
0:34 we've accepted their data. It was all good. And we could either give them a location where it was created or something that describes
0:42 it. And what we're doing to describe it here is this is you know, theoretically an ID You could do a lookup on in a database or some
0:48 other API right? That's the idea, we're not fully fledged in this out. But a 201 created makes a lot more sense than 200.
0:55 So does that mean we've got to go write more complicated code instead of just doing this?
0:59 We've got a create a response and set all the information about it? No. Just like this little name up here, we could just say "status code equals 201".
1:07 So if everything goes okay, that's what's getting submitted, or returned. Otherwise, it's whatever you explicitly set. So like we would up here,
1:16 you know, 400 or 500 or whatever. Let's just submit this again. Boom. Check it out. 201
1:22 created. The request has been fulfilled and resulted in a new resource being created. And guess what? That new resource was returned as well. Awesome.
1:29 Now we're playing nice with status codes.

Talk Python's Mastodon Michael Kennedy's Mastodon