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.
0:03
If we open it up down here,
0:04
let's see what we got. Remember,
0:06
we have a not found, but if we go to "api/calculate" we're getting
0:09
this data back and it's 4.
0:11
Well, how exciting and how generally useful is this calculator?
0:15
Not at all, right? All we're ever going to get is 4.
0:18
So the answer is always 4. Maybe it should have been 42, but 4 it is.
0:21
So let's go and change this so we can pass some data in.
0:25
Now let's say we take an "x, y, and z",
0:29
those are the three things we could pass in, and the value is gonna be
0:33
"x + y" like, for now we'll do times, maybe we'll do divide,
0:37
It will let us do something slightly more interesting in just a moment. So we could
0:41
go over here, and if we run this again,
0:43
we come up and say
0:44
"?x=2&y=3&z=10"
0:49
What are we going to get here?
0:50
Are we going to get, what did we say?
0:52
X plus y is 5, and then times 10 it should be 50, are we gonna get 50?
0:56
Let's find out. No,
0:58
we got nothing. So we cannot multiply sequence by a string.
1:03
Wait a minute. What's going on here?
1:04
Let's take away the Z for a minute.
1:06
Try that again. 23, 23 as a string.
1:10
If you look at it real careful,
1:11
notice right here, there are quotes, that is not an integer
1:14
in JSON, that is a string.
1:16
So what's the problem? The problem is,
1:18
everything passed around on the Web is strings by default.
1:22
But something needs to say "no,
1:23
no, no. We expect this to be an integer".
1:26
So if I go over here and I change this and I use type hints, you
1:30
say this is an integer and this is an integer and we say this
1:34
is an integer. Let's do this again.
1:36
Also notice it didn't say anything about any of these values being required.
1:41
But now, let's try it again.
1:43
Well check it out. We got the value
1:44
5, and if we look at the raw data,
1:47
it doesn't have quotes, it's really 5.
1:48
And let's go back here and put our times z, run it again,
1:52
Yes, we got 50. Okay,
1:54
this is working pretty well. What happens if I omit Z?
1:59
Look at that. We got an error message and if we actually look exactly what
2:01
we got, it says "there's something wrong with the query string" and there's a Z
2:06
that's supposed to be in there.
2:07
But there's no Z in the query string,
2:08
but we expected one, and the problem is the Z is required and the type
2:12
of errors is that it's missing. So let's put our Z back.
2:18
Okay, so you're gonna see when we pass these over,
2:20
If there's no default value and there's no optional value,
2:23
things like that, these become required.
2:25
But we could say over here that this is a 10.
2:29
Here we go. Now, we don't have to specify the Z because there's a
2:32
default value for it. But if we do for the omit
2:35
the y where there is no default value then hey,
2:36
guess what? y is missing.
2:38
We gotta pass it over. Alright,
2:39
cool. So this is how we pass data at least from the query string as
2:44
well as you would see the path, like we could do like X,
2:47
Y like this and make that part of the URL, doesn't have to be the query
2:51
string, and you would be doing this exactly the same way.
2:54
Okay, so this is how we pass data over to our API method.