Modern APIs with FastAPI and Python Transcripts
Chapter: Course conclusion and review
Lecture: Review: Status codes and responses
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
When everything goes right, all we have to do is return some piece of data,
0:03
maybe a pydantic data model or a dictionary or something like that,
0:07
And FastAPI will convert that over to the data that we're going to
0:11
send back in JSON typically, and it's gonna send a 200
0:15
all ok status could. But what happens if something goes wrong?
0:18
We can't just return a different style of data.
0:21
That's not enough. So what we're gonna do is actually return explicit responses from
0:27
FastAPI. So here what we're doing is we're saying we're gonna try to
0:30
do this processing and return
0:32
a dictionary correctly. And if for some reason that doesn't work,
0:35
we'll catch any validation errors and say,
0:38
here is the error message and the status code,
0:40
which is usually 400, but sometimes it's 404. And if something else goes wrong, like
0:45
nothing we expected, but the server still crashed,
0:47
we can convert that over to something like error processing,
0:50
request status code 500 and log it and then send that back.
0:54
So be sure to leverage this ability to send back meaningful http Status codes.
1:00
Remember, "httpstatuses.com" tells you all about the different options, and you can go
1:05
over there and pick the right one and use this style of programming here to send
1:09
that back and have a meaningful, nice,
1:12
well-behaved API. When something goes wrong,
1:14
it tells you what went wrong rather than just crashing.