Modern APIs with FastAPI and Python Transcripts
Chapter: Building our first API
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.
0:04 That was great. And you can actually control it up here by setting the status
0:08 code to say, like, 201 for some kind of post or something along those
0:12 lines. But what if we change this a little bit and we put a divide
0:16 here and let's go ahead and change this to an optional, we gotta import that, oops not
0:21 like that, import it like this, an optional integer,
0:25 and by default, there's nothing there.
0:27 If we're going to return, or we're going to compute this,
0:30 how well do you think dividing
0:32 say the number 5 by none is gonna turn out?
0:34 Not so well. And let's go a little step further
0:37 and let's actually kind of echo back the arguments as we saw them,
0:41 right? They pass something we want to say
0:43 "this is what we got from what you sent us".
0:45 We'll put our X, Y, and Z here,
0:47 so we're gonna need to do some sort of test,
0:50 right? Value is gonna be X plus Y.
0:52 And then we'll say, if Z is not none,
0:54 you might say if not Z,
0:55 but yeah, if it's not Z,
0:58 and if it's equal to zero, maybe we want to respond some other way.
1:02 So if it's not none,
1:04 we're going to say "value /="
1:08 you know, divide value by Z.
1:10 They're going to return it. Let's try this.
1:12 Alright, we do our request. Right now, Let's
1:14 put a Z back first, Z equals 10, like
1:18 it was before. Perfect. So we got 2
1:20 + 3 is five. Divide it by 10 is 0.5,
1:23 everything's great. Well, now that Z is in there,
1:26 what if we say we omit
1:28 Z, that's okay, it doesn't crash,
1:29 it just doesn't do the calculation.
1:31 But what if we put in Z equals zero?
1:34 That explicitly is not good. It's not that the Z was required,
1:38 It's just that this value of Z is bad data.
1:41 So how are we going to communicate that back to them?
1:44 So let's go over here and right at the beginning,
1:46 we'll do a test. we'll say,
1:48 look, if you passed in a Z and it's exactly the number zero, well,
1:52 this value is just not gonna work for us,
1:55 right? How do we communicate that?
1:57 Could you come over here and say "error:
1:59 Zero Z"? right? Uh,
2:02 return that. We return that. Well,
2:04 yes, we technically could return it,
2:06 but what status code is it going to send?
2:08 What message is it going to communicate to the client?
2:11 200. Everything's okay. So yeah,
2:14 not really. Like we could do this,
2:16 but it is not going to communicate well with the API endpoints.
2:19 Like especially think of a program trying to talk to this API,
2:22 how is it going to know which is which,
2:24 Right? So what we want to do is return a "fastapi.response"
2:27 and then we could just set two things,
2:30 we can set the content to "Error: Z cannot be zero",
2:35 and then the status code, here's our http statuses,
2:38 And let's just say 400 bad request,
2:41 right? So we're gonna come in here and say,
2:43 Look, if you give us zero, this is totally bad. We cannot continue.
2:46 It's gonna crash. But we don't want our server to crash.
2:49 We want to say the reason this has gone wrong is the data you supplied,
2:53 not 500 server error, which we saw up here, internal server error, this looks like the
2:58 service broke, not like they gave us bad data.
3:01 It's also very unhelpful to them.
3:03 So if we just rerun this now we get an error.
3:07 And more importantly, more importantly,
3:10 if we look at the network, 400 bad request,
3:14 right, 400 bad request. But if we put like 1, now we get a 200
3:18 ok, we get our value back.
3:20 And if we put no Z, that's fine,
3:22 we get 200, Just a different calculation.
3:25 So when we want to communicate
3:26 one of these non standard responses, not
3:29 everything's okay and here's your data,
3:31 we're going to create a FastAPI response, set
3:34 its status code, potentially set its message.
3:37 Now, one of the things you might want to do, might want to consider here
3:40 I'm not going to do another simple example,
3:42 Maybe we'll do it later, is if you look at this over here,
3:47 get our Z equals zero back, so what came back is 400 but the type it
3:53 came back is plain text, whereas the normal response like this,
3:59 this comes back as JSON.
4:00 Let me just do it up here so you can see it actually in the network tools
4:03 right? This is coming back as content
4:07 application/json. So what you might consider doing is creating a dictionary in
4:12 here like this, did I say I wasn't gonna do this?
4:16 Let's just do this like that.
4:18 It's a string, not not some JSON.
4:21 And then we come over here and we could say media type equals application/
4:26 json, like so, and put the comments in the right place.
4:30 There we go. And that went a little wonky,
4:34 didn't it? Okay, weird indentation got in there,
4:42 but let's go try this again.
4:45 Here we got back this application is JSON.
4:48 Everything was okay, but if we got an error,
4:52 notice the data we get back
4:54 is still JSON. This, we could look at that.
4:57 The response is still JSON. So that way the clients assume it can always just
5:01 get the document and look at what they got back.
5:04 It's not like sometimes it's text,
5:06 sometimes it's JSON. I don't know,
5:08 I think that's kind of nicer.
5:09 It's up to you guys whether or not you want to follow up on that and
5:11 do this. But by default
5:13 FastAPI returns errors that are not controlled by you as JSON. Like,
5:18 remember, if we click here,
5:19 we get a 404, but the 404 is actually a JSON Document.
5:23 Plus the 404. So maybe it makes sense for us to factor that
5:27 into our error messages as well,
5:30 and pass over this application JSON.