Requests and Responses are crucial parts of a HTTP framework - the request contains data that is received from the client, and the Response contains data that is sent to the Client.
Kyoukai piggybacks off of Werkzeug for its request and response wrappers - this means that most of the form logic and etc is handled by a well tested library used in thousands of applications across the web.
The Request
object for the current request is available on
request
for your route functions to use.
For example, returning a JSON blob of the headers:
async def my_route(ctx: HTTPRequestContext):
headers = json.dumps(ctx.headers)
return headers
Responses are automatically created for you when you return from a route function or error handler. However, it is possible to create them manually:
async def my_route(ctx: HTTPRequestContext):
return Response("Hello, world", status=200)
New in version 2.1.3.
There are some built-in helper functions to encode data in a certain form:
kyoukai.util.
as_html
(text, code=200, headers=None)[source]Returns a HTML response.
return as_html("<h1>Hel Na</h1>", code=403)
Parameters: | |
---|---|
Return type: | |
Returns: | A new |
kyoukai.util.
as_plaintext
(text, code=200, headers=None)[source]Returns a plaintext response.
return as_plaintext("hel yea", code=201)
Parameters: | |
---|---|
Return type: | |
Returns: | A new |
kyoukai.util.
as_json
(data, code=200, headers=None, *, json_encoder=None)[source]Returns a JSON response.
return as_json({"response": "yes", "code": 201}, code=201)
Parameters: | |
---|---|
Return type: | |
Returns: | A new |