Anvil: Web Apps with Nothing but Python Transcripts
Chapter: Adding APIs and HTTP Endpoints
Lecture: Passing data to the API method
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well, we created our ACPM point but let's explore just a little bit about how we might pass data. There's a number of ways in which you could
0:09
pass data to the API. Over here we're just calling /hello and we just get a basic response back. We could pass something like hello your name
0:21
something like this. We could have query parameters x equals seven and maybe less obviously 'cause it's hard to do here
0:30
but we could have a body element which is like a JSON document itself that we're submitting back. So, all three of those are ways
0:38
that we could pass data over. And let's just play with those real quick. So, if we want to have like /name like slash Sarah or whatever
0:46
we can say :name here and we can put back the name element there. And, if we want to capture the query parameters call this query params
0:59
like that, and we get this. And if we want to get the JSON body I'll just put this here. This will be anvil.server.request.
1:05
It's how we get a bunch of stuff about the inbound request. And we can say body_json. So let's echo the other ones back. Okay, so let's just save that.
1:16
That'll refresh this. And we come over here we can say hello Sarah. Like that. And we see it says, "Hello Sarah," so it's passed over.
1:28
There's no body passed over but we do have the query as x equals seven and we could also have y equals 10. So we, this actually, if you look at the raw
1:37
this comes in as basically a little sub-dictionary with keys and values So that's how we pass data over. I said we can't really easily pass data in
1:46
using the browser, just like this, for the body. We'll see how to do that when we build the real meaningful API for our app.