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,
0:02
we did this post. We passed some data that was in the body.
0:05
It created this entity over here and everything worked.
0:09
So what did FastAPI do? It said "great,
0:11
everything worked. 200 ok". But let's drop over on our httpstatuses.com again
0:17
and see about these success codes. So Okay,
0:20
well, that's kind of the most generic term.
0:22
And here's a representation of the resource.
0:24
We actually did give that back, so
0:26
it's kind of okay. But what would make more sense would be created. Over
0:31
here, we're creating a new thing,
0:33
we've accepted their data. It was all good.
0:36
And we could either give them a location where it was created or something that describes
0:41
it. And what we're doing to describe it here is this is you know,
0:43
theoretically an ID You could do a lookup on in a database or some
0:47
other API right? That's the idea,
0:49
we're not fully fledged in this out.
0:51
But a 201 created makes a lot more sense than 200.
0:54
So does that mean we've got to go
0:56
write more complicated code instead of just doing this?
0:58
We've got a create a response and set all the information about it?
1:02
No. Just like this little name up here,
1:04
we could just say "status code equals 201".
1:06
So if everything goes okay, that's what's getting submitted, or returned.
1:11
Otherwise, it's whatever you explicitly set.
1:13
So like we would up here,
1:15
you know, 400 or 500 or whatever.
1:17
Let's just submit this again. Boom.
1:20
Check it out. 201
1:21
created. The request has been fulfilled and resulted in a new resource being created.
1:25
And guess what? That new resource
1:26
was returned as well. Awesome.
1:28
Now we're playing nice with status codes.