#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: FastAPI
Lecture: Responding to requests
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We saw that by default, our API method is going to return 200. That was great.
0:06
And you can actually control it up here by setting the status code to say like 201 for some kind of post or something along those lines.
0:14
But what if we change this a little bit and we put a divide here and let's go ahead and change this to an optional. Import that. Oops, not like that.
0:23
Import it like this. An optional integer. And by default, there's nothing there. If we're going to return, we're going to compute this.
0:31
How well do you think dividing, say, the number five by none is going to turn out? Not so well. And let's go a little step farther.
0:39
And let's actually kind of echo back the arguments as we saw them, right?
0:43
They pass something we want to say, this is what we got from what you sent us. We'll put our X, Y and Z here.
0:49
So we're going to need to do some sort of test, right? Value is going to be X plus Y.
0:53
And then we'll say if Z is not none, you might say if not Z, but yeah, if it's none,
0:58
if not Z and if it's equal to zero, maybe we want to respond some other way.
1:03
So if it's not none, we're going to say value slash equals, you know, divide value by Z. And we're going to return it. Let's try this.
1:13
All right, we do a request right now. Let's put a Z back first. Z equals 10, like it was before. Perfect.
1:21
So we got two plus three is five, divided by 10 is 0.5. Everything's great. Now that Z is in there, what if we say we omit Z?
1:30
That's okay, it doesn't crash. It just doesn't do the calculation. But what if we put in Z equals zero? That explicitly is not good.
1:38
It's not that the Z was required. It's just that this value of Z is bad data. So how are we going to communicate that back to them?
1:45
So let's go over here and right at the beginning, we'll do a test.
1:48
We'll say, look, if you passed in a Z and it's exactly the number zero, well, this value is just not going to work for us, right?
1:58
How do we communicate that? Could you come over here and say error zero Z, right? Return that. We return that.
2:05
Well, yes, we technically could return it, but what status code is it going to send? What message is it going to communicate to the client?
2:13
200, everything's okay. So yeah, not really. We could do this, but it's not going to communicate well with the API endpoints.
2:20
Especially think of a program trying to talk to this API. How is it going to know which is which, right?
2:26
So what we want to do is return a FastAPI dot response, and then we can just set two things. We can set the content to error Z cannot be zero.
2:37
And then the status code, here's our HTTP statuses. And let's just say 400 bad request, right?
2:42
So we're going to come in here and we'll say, look, if you give us zero, this is totally bad. We cannot continue.
2:48
It's going to crash, but we don't want our server to crash.
2:50
We want to say the reason this has gone wrong is the data you supplied, not 500 server error, which we saw up here, internal server error.
2:59
This looks like the service broke, not like they gave us bad data. Okay. It's also very unhelpful to them.
3:04
So if we just rerun this, now we get an error. And more importantly, more importantly, if we look at the network, 400 bad request, right?
3:15
400 bad requests. But if we put like one, now we get a 200 A okay, and we get our value back. And if we put no Z, that's fine.
3:23
Here are 200, just a different calculation. So when we want to communicate one of these non-standard responses, not everything's okay,
3:31
and here's your data, we're going to create a FastAPI response, set its status code, potentially set its message.
3:38
Now one of the thing you might want to do, might want to consider here, I'm not going to do it in this simple example. Maybe we'll do it later.
3:45
Is if you look at this over here, our Z equals zero back. So what came back is 400, but the type it came back is plain text.
3:56
Whereas the normal response like this, this comes back as JSON. Let me just do it up here so you can see it actually in the network tools, right?
4:06
This is coming back as content application slash JSON. So what you might consider doing is creating a dictionary in here like this.
4:15
I said I wasn't going to do this, let's just do this. Like that. It's a string, not some JSON.
4:22
And then we come over here and we could say media type equals application slash JSON, like so. And put the commas in the right place. There we go.
4:32
And that went a little wonky, didn't it? Okay, a weird indentation got in there, but let's go try this again.
4:46
Here we got back this application is JSON, everything was okay. If we got an error, notice the data we get back is still JSON.
4:57
This we can look at that. The response is still JSON. So that way the clients consuming it can always just get the document and look at what they
5:04
got back. It's not like sometimes it's text, sometimes it's JSON. I don't know, I think that's kind of nicer.
5:10
It's up to you guys whether or not you want to follow up on that and do this.
5:13
But by default, FastAPI returns errors that are not controlled by you as JSON.
5:19
Like remember, if we click here, we get a 404, but the 404 is actually a JSON document plus the 404.
5:26
So maybe it makes sense for us to factor that into our error message as well and pass over this application in JSON.