#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: FastAPI
Lecture: Passing data to the API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So our calculator API is working. We open it up down here, let's see what we got.
0:06 Remember we have a not found, but if we go to API/calculate, we're getting this data back and it's four.
0:12 Well how exciting and how generally useful is this calculator? Not at all, right? All we're ever going to get is four, so the answer is always four.
0:20 Maybe it should have been 42, but four it is. So let's go and change this so we can pass some data in.
0:26 Now let's say we take an X, a Y, and a Z. Those are three things we could pass in. And the value is going to be X plus Y, like this.
0:36 For now we'll do times. Maybe we'll do divide, it'll let us do something slightly more interesting in just a moment.
0:41 So we can go over here and if we run this again, we come up and say question mark, X equals two, Y equals three, and Z equals ten.
0:51 What are we going to get here? Are we going to get, what did we say, X plus Y is five, and then times ten, this should be 50. Are we going to get 50?
0:58 Let's find out. No, we got nothing. So we cannot multiply a sequence by a string. Wait a minute, what's going on here?
1:06 Let's take away the Z for a minute, try that again. Two, three, 23, as a string. If you look at it real careful, notice right here there are quotes.
1:14 That is not an integer in JSON, that is a string. So what's the problem? The problem is everything passed around on the web is strings by default.
1:23 But something needs to say, no, no, no, we expect this to be an integer.
1:27 So if I go over here and I change this, and I use type hints, we say this is an integer, and this is an integer, and we say this is an integer.
1:37 Let's do this again. Also notice it didn't say anything about any of these values being required. But now, let's try it again.
1:44 Check it out, we got the value five. If we look at the raw data, it doesn't have quotes, it's really a five.
1:50 Let's go back here and put R times Z, run it again. Yes, we got 50. Okay, this is working pretty well. What happens if I omit the Z?
1:59 Look at that, we got an error message. And if we actually look at exactly what we got, it says there's something wrong with
2:04 the query string, and there's a Z that's supposed to be in there, but there's no Z in the query string, but we expected one.
2:11 And the problem is the Z is required, and the type of error is that it's missing. So let's put our Z back.
2:17 Okay, so you're going to see when we pass these over, if there's no default value and
2:23 there's no optional value, things like that, these become required. But we could say over here that this is a 10. There we go.
2:31 Now we don't have to specify the Z because there's a default value for it, but if we
2:34 do for the, omit the Y, where there is no default value, then hey, guess what? Y is missing, we've got to pass it over. Alright, cool.
2:41 So this is how we pass data, at least from the query string, as well as you would see
2:46 the path, like we could do like X, Y, like this, and make that part of the URL.
2:52 Doesn't have to be the query string, and you'd be doing this exactly the same way. Okay, so this is how we pass data over to our API method.


Talk Python's Mastodon Michael Kennedy's Mastodon