Skip to content

Responses

Lilya, by design, furnishes specific response classes that serve a dual purpose. They offer utility and are tasked with sending the appropriate ASGI messages through the send channel.

Lilya automatically includes the Content-Length and Content-Type headers.

How does it work

There are a few ways of using the responses within a Lylia application.

Available responses

All the responses from Lilya inherit from the parent object Response and that same class can also be used directly.

All the responses are considered ASGI applications, which means you can treat them as such in your application if necessary.

Example

from lilya.responses import PlaiText
from lilya.types import Scope, Receive, Send


async def asgi_app(scope: Scope, receive: Receive, send: Send):
    assert scope['type'] == 'http'
    response = PlaiText('Welcome')
    await response(scope, receive, send)

Response

from lilya.responses import Response

Example

from lilya.apps import Lilya
from lilya.responses import Response
from lilya.routing import Path


def home():
    return Response("Welcome home")


app = Lilya(routes=[Path("/", home)])

Lilya provides the set_cookie that allows settings a cookie on a given response. All the responses available in Lilya have access to this functionality.

from lilya.responses import Response
from lilya.types import Scope, Receive, Send


async def asgi_app(scope: Scope, receive: Receive, send: Send):
    assert scope['type'] == 'http'
    response = Response('Welcome', media_type='text/plain')

    response.set_cookie(key=..., value=..., max_age=..., expires=...,)
    await response(scope, receive, send)
Parameters

The available parameters of the set_cookie are as follow:

  • key - A string representing the cookie's key.
  • value - A string representing the cookie's value.
  • max_age - An integer defining the cookie's lifetime in seconds. A negative value or 0 discards the cookie immediately. (Optional)
  • expires - Either an integer indicating the seconds until the cookie expires or a datetime. (Optional)
  • path - A string specifying the subset of routes to which the cookie applies. (Optional)
  • domain - A string specifying the valid domain for the cookie. (Optional)
  • secure - A boolean indicating that the cookie is sent to the server only if the request uses SSL and the HTTPS protocol. (Optional)
  • httponly - A boolean indicating that the cookie is inaccessible via JavaScript through Document.cookie, the XMLHttpRequest, or Request APIs. (Optional)
  • samesite - A string specifying the samesite strategy for the cookie, with valid values of 'lax', 'strict', and 'none'. Defaults to 'lax'. (Optional)

In the same fashion as the set cookie, this function is available on every response provided by Lilya.

from lilya.responses import Response
from lilya.types import Scope, Receive, Send


async def asgi_app(scope: Scope, receive: Receive, send: Send):
    assert scope['type'] == 'http'
    response = Response('Welcome', media_type='text/plain')

    response.delete_cookie(key=..., path=..., domain=...)
    await response(scope, receive, send)
Parameters

The available parameters of the set_cookie are as follow:

  • key - A string representing the cookie's key.
  • path - A string specifying the subset of routes to which the cookie applies. (Optional)
  • domain - A string specifying the valid domain for the cookie. (Optional)

HTMLResponse

Returning an html response.

from lilya.responses import HTMLResponse

Example

from lilya.apps import Lilya
from lilya.responses import HTMLResponse
from lilya.routing import Path


def home():
    return HTMLResponse("<html><body><p>Welcome!</p></body></html>")


app = Lilya(routes=[Path("/", home)])

Error

Response that can be used when throwing a 500 error. Defaults to return an html response.

from lilya.responses import Error

Example

from lilya.apps import Lilya
from lilya.responses import Error
from lilya.routing import Path


def home():
    return Error("<html><body><p>Error!</p></body></html>")


app = Lilya(routes=[Path("/", home)])

PlainText

Response that can be used to return text/plain.

from lilya.responses import PlainText

Example

from lilya.apps import Lilya
from lilya.responses import PlainText
from lilya.routing import Path


def home():
    return PlainText("Welcome home")


app = Lilya(routes=[Path("/", home)])

JSONResponse

Response that can be used to return application/json.

from lilya.responses import JSONResponse

Example

from lilya.apps import Lilya
from lilya.responses import JSONResponse
from lilya.routing import Path


def home():
    return JSONResponse({"message": "Welcome home"})


app = Lilya(routes=[Path("/", home)])

Ok

Response that can be used to return application/json as well. You can see this as an alternative to JSONResponse.

from lilya.responses import Ok

Example

from lilya.apps import Lilya
from lilya.responses import Ok
from lilya.routing import Path


def home():
    return Ok({"message": "Welcome home"})


app = Lilya(routes=[Path("/", home)])

RedirectResponse

Used for redirecting the responses.

from lilya.responses import RedirectResponse

Example

from lilya.apps import Lilya
from lilya.responses import RedirectResponse
from lilya.routing import Path


def home():
    return RedirectResponse(url="/another-url")


app = Lilya(routes=[Path("/", home)])

StreamingResponse

from lilya.responses import StreamingResponse

Example

from collections.abc import Generator

from lilya.apps import Lilya
from lilya.responses import StreamingResponse
from lilya.routing import Path


def my_generator() -> Generator[str, None, None]:
    count = 0
    while True:
        count += 1
        yield str(count)


def home():
    return StreamingResponse(my_generator(), media_type="text/html")


app = Lilya(routes=[Path("/", home)])

FileResponse

from lilya.responses import FileResponse

Streams a file asynchronously as the response, employing a distinct set of arguments for instantiation compared to other response types:

  • path - The filepath to the file to stream.
  • status_code - The Status code to return.
  • headers - Custom headers to include, provided as a dictionary.
  • media_type - A string specifying the media type. If unspecified, the filename or path is used to deduce the media type.
  • filename - If specified, included in the response Content-Disposition.
  • content_disposition_type - Included in the response Content-Disposition. Can be set to attachment (default) or inline.
  • background - A task instance.

Example

from lilya.apps import Lilya
from lilya.responses import FileResponse
from lilya.routing import Path


def home():
    return FileResponse(
        "files/something.csv",
        filename="something",
    )


app = Lilya(routes=[Path("/", home)])

Importing the appropriate class

This is the classic most used way of using the responses. The available responses contains a list of available responses of Lilya but you are also free to design your own and apply them.

Example

from lilya.apps import Lilya
from lilya.responses import JSONResponse
from lilya.routing import Path


def home():
    return JSONResponse({"message": "Welcome home"})


app = Lilya(routes=[Path("/", home)])

Build the Response

This is where the things get great. Lilya provides a make_response function that automatically will build the response for you.

from lilya.responses import make_response

Example

from lilya.apps import Lilya
from lilya.responses import make_response
from lilya.routing import Path


def home():
    return make_response({{"message": "Hello"}}, status_code=201)


app = Lilya(routes=[Path("/", home)])

By default, the make_response returns a JSONResponse but that can be also changed if the response_class parameter is set to something else.

So, why is this make_response different from the other responses? Well, here its where Lilya shines.

Lilya is pure Python, which means that it does not rely or depend on external libraries like Pydantic, msgspec, attrs or any other but allows you to build a custom encoder that can later be used to serialise your response automatically and then passed to the make_response.

Check the build a custom encoder and custom encoders with make_response for more details and how to leverage the power of Lilya.

Delegate to Lilya

Delegating to Lilya means that if no response is specified, Lilya will go through the internal encoders and will try to jsonify the response for you.

Let us see an example.

from lilya.apps import Lilya
from lilya.routing import Path


def home():
    return {"message": "Welcome home"}


app = Lilya(routes=[Path("/", home)])

As you can see, no response was specified but instead a python dict was returned. What Lilya internally does is to guess and understand the type of response parse the result into json and returning a JSONResponse automatically,

If the type of response is not json serialisable, then a ValueError is raised.

Let us see some more examples.

from lilya.apps import Lilya
from lilya.routing import Path


def home_dict():
    return {"message": "Welcome home"}


def home_frozen_set():
    return frozenset({"message": "Welcome home"})


def home_set():
    return set({"message": "Welcome home"})


def home_list():
    return ["Welcome", "home"]


def home_str():
    return "Welcome home"


def home_int():
    return 1


def home_float():
    return 2.0


app = Lilya(
    routes=[
        Path("/dict", home_dict),
        Path("/fronzenset", home_frozen_set),
        Path("/set", home_set),
        Path("/list", home_list),
        Path("/str", home_str),
        Path("/int", home_int),
        Path("/float", home_float),
    ]
)

And the list goes on and on. Lilya by design understands almost every single datastructure of Python by default, including Enum, deque, dataclasses, PurePath, generators and tuple.

This is archived by using Encoders. Mainly they are additional encoders for types tje json encoder cannot handle.