Modern APIs with FastAPI and Python Transcripts
Chapter: Building our first API
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. If we open it up down here, let's see what we got. Remember,
0:07 we have a not found, but if we go to "api/calculate" we're getting this data back and it's 4.
0:12 Well, how exciting and how generally useful is this calculator? Not at all, right? All we're ever going to get is 4.
0:19 So the answer is always 4. Maybe it should have been 42, but 4 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, y, and z", those are the three things we could pass in, and the value is gonna be
0:34 "x + y" like, for now we'll do times, maybe we'll do divide, It will let us do something slightly more interesting in just a moment. So we could
0:42 go over here, and if we run this again, we come up and say "?x=2&y=3&z=10" What are we going to get here? Are we going to get, what did we say?
0:53 X plus y is 5, and then times 10 it should be 50, are we gonna get 50? Let's find out. No, we got nothing. So we cannot multiply sequence by a string.
1:04 Wait a minute. What's going on here? Let's take away the Z for a minute. Try that again. 23, 23 as a string. If you look at it real careful,
1:12 notice right here, there are quotes, that is not an integer in JSON, that is a string. So what's the problem? The problem is,
1:19 everything passed around on the Web is strings by default. 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, you say this is an integer and this is an integer and we say this
1:35 is an integer. 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 Well check it out. We got the value 5, and if we look at the raw data, it doesn't have quotes, it's really 5.
1:49 And let's go back here and put our times z, run it again, Yes, we got 50. Okay, this is working pretty well. What happens if I omit Z?
2:00 Look at that. We got an error message and if we actually look exactly what
2:02 we got, it says "there's something wrong with the query string" and there's a Z that's supposed to be in there. But there's no Z in the query string,
2:09 but we expected one, and the problem is the Z is required and the type of errors is that it's missing. So let's put our Z back.
2:19 Okay, so you're gonna see when we pass these over, If there's no default value and there's no optional value, things like that, these become required.
2:26 But we could say over here that this is a 10. Here we go. Now, we don't have to specify the Z because there's a
2:33 default value for it. But if we do for the omit the y where there is no default value then hey, guess what? y is missing.
2:39 We gotta pass it over. Alright, cool. So this is how we pass data at least from the query string as
2:45 well as you would see the path, like we could do like X, Y like this and make that part of the URL, doesn't have to be the query
2:52 string, and you would 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