Anvil: Web apps with nothing but Python Transcripts
Chapter: Adding APIs and HTTP Endpoints
Lecture: Passing data to the API method
0:00 Well, we created our ACPM point
0:02 but let's explore just a little bit
0:03 about how we might pass data.
0:06 There's a number of ways in which you could
0:08 pass data to the API.
0:11 Over here we're just calling /hello
0:14 and we just get a basic response back.
0:17 We could pass something like hello your name
0:20 something like this.
0:21 We could have query parameters x equals seven
0:26 and maybe less obviously
0:28 'cause it's hard to do here
0:29 but we could have a body element
0:31 which is like a JSON document itself
0:34 that we're submitting back.
0:36 So, all three of those are ways
0:37 that we could pass data over.
0:39 And let's just play with those real quick.
0:40 So, if we want to have like /name
0:43 like slash Sarah or whatever
0:45 we can say :name here
0:47 and we can put back the name element there.
0:50 And, if we want to capture the query parameters
0:55 call this query params
0:58 like that, and we get this.
1:00 And if we want to get the JSON body
1:01 I'll just put this here.
1:03 This will be anvil.server.request.
1:04 It's how we get a bunch of stuff about the inbound request.
1:09 And we can say body_json.
1:11 So let's echo the other ones back.
1:14 Okay, so let's just save that.
1:15 That'll refresh this.
1:17 And we come over here
1:18 we can say hello Sarah.
1:22 Like that.
1:23 And we see it says, "Hello Sarah,"
1:25 so it's passed over.
1:27 There's no body passed over
1:28 but we do have the query as x equals seven
1:31 and we could also have y equals 10.
1:34 So we, this actually, if you look at the raw
1:36 this comes in as basically a little sub-dictionary
1:39 with keys and values
1:41 So that's how we pass data over.
1:42 I said we can't really easily pass data in
1:45 using the browser, just like this, for the body.
1:48 We'll see how to do that when we build
1:49 the real meaningful API for our app.