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


Talk Python's Mastodon Michael Kennedy's Mastodon