Requests and Responses

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.

Getting the Request

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

Creating a Response

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)

Response Helpers

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:
  • text (str) – The text to return.
  • code (int) – The status code of the response.
  • headers (Optional[dict]) – Any optional headers.
Return type:

Response

Returns:

A new werkzeug.wrappers.Response representing the HTML.

kyoukai.util.as_plaintext(text, code=200, headers=None)[source]

Returns a plaintext response.

return as_plaintext("hel yea", code=201)
Parameters:
  • text (str) – The text to return.
  • code (int) – The status code of the response.
  • headers (Optional[dict]) – Any optional headers.
Return type:

Response

Returns:

A new werkzeug.wrappers.Response representing the text.

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:
  • data (Union[dict, list]) – The data to encode.
  • code (int) – The status code of the response.
  • headers (Optional[dict]) – Any optional headers.
  • json_encoder (Optional[JSONEncoder]) – The encoder class to use to encode.
Return type:

Response

Returns:

A new werkzeug.wrappers.Response representing the JSON.