-
When I checked the official demo recently, I found that this code can't start the route and the parameter is missing import uvicorn
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import Response, PlainTextResponse
from starlette.routing import Route
async def apps(scope, receive, send):
assert scope['type'] == 'http'
request = Request(scope, receive)
content = '%s %s' % (request.method, request.url.path)
response = Response(content, media_type='text/plain')
await response(scope, receive, send)
def text(request):
return PlainTextResponse('Hello')
routes = [
Route("/s/{s}", endpoint=apps),
Route("/t/{t}", endpoint=text),
]
app = Starlette(routes=routes)
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8082) error code
The routing service doesn't seem to inherit receive and send, and can't go down |
Beta Was this translation helpful? Give feedback.
Answered by
jhominal
Jul 10, 2022
Replies: 1 comment
-
As There is more information in the documentation, in the Routing section. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Kludex
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Route.endpoint
currently accepts either:starlette.requests.Request
and returns astarlette.responses.Response
object (like yourtext
),__init__
that implements the ASGI interface (which you can use by e.g. subclassingstarlette.endpoints.HTTPEndpoint
).As
apps
is not in either case, it doesn’t work withRoute.endpoint
as currently implemented by Starlette.There is more information in the documentation, in the Routing section.