Last edited 2023-09-09 16:25:58.114593+00:00
You can add a query string parameter to your route via the query
decorator:
from view import new_app, query
app = new_app()
@app.get("/hello")
@query("name", str)
async def hello(name: str):
return p(f"Hello, {name}")
app.run()
The first argument is the name of the parameter in the query string, not the argument name, and the second argument is the type that it should take.
Danger
view.py has not yet implemented type checking on parameters
Bodies work the exact same way, but with the body
decorator instead:
@app.get("/goodbye")
@body("name", str)
async def goodbye(name: str):
return p(f"Bye, {name}")
Warning
As of now, only bodies sent in JSON are supported.
Path parameters are even simpler, just wrap a route part in brackets, like so:
@app.get("/hello/{name}")
async def hello(name: str):
return p(f"Your name is: {name}")
Now in the browser, if you were to go to /hello/world
, the name
parameter above would be world
.
Here's a more complicated example:
@app.get("/auth/{user_id}/something/{token}")
async def token(user_id: str, token: str):
# ...
return p("Successfully authorized!")
Danger
This is extremely buggy and not yet recommended for general use.