Replies: 3 comments 10 replies
|
This is a very good initiative. class MyRequest(Request): ...
def index(request: MyRequest): ...
routes = [
Route('/', index)
]This is not a breaking solution and does not affect the performance because you extract class type once in the |
2 replies
|
app = Starlette(request_parsers=[json_parser, form_parser, ...])
# Request
async def parse(self) -> Any:
for parser in self.app.request_parsers:
if parser.supports(self.headers['content-type']):
return await parser.parse(...)
raise
async def json(self) -> Any:
parser = get_json_parser(self.app.request_parsers)
return await parser.parse(...)See https://www.django-rest-framework.org/api-guide/content-negotiation/ |
8 replies
This comment was marked as off-topic.
This comment was marked as off-topic.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
There were a lot of discussions in the past around:
RequestandWebSocketclassesThere's an extensive list of related issues around it:
This discussion is for us to think about an API that would satisfy all those issues, and that it doesn't bring a huge complexity to the source code.
Suggestions
request_classparameterHaving a
request_classparameter on theStarlette,RouterandRouteclasses.request_parsersparameterThis idea is motivated from some comments that @tomchristie made around, that I guess are inspired on DRF: create an API for Starlette to use custom parsers that are used depending on the content type.
Since
Requestclass is an ASGI application by itself, adding theparsersparameter over there would break this interface, so I guess we'd have some global configuration like we have for convertors:https://github.com/encode/starlette/blob/33f46a13625bcca4b7520e33be299a23b2e2b26c/starlette/convertors.py#L86-L87
I don't like this. Is there an alternative API we can create here?
Add
json_loadsparameter andparsersparameters to methodsWe have the following methods:
Request.json()Request.form()WebSocket.receive_json()The idea would be to add
json_loadsparameter toRequest.json()andWebSocket.receive_json(), andparsersparameter toRequest.form().To discuss
I actually touched superficially the subject here, I'd like for us to discuss it. cc @encode/maintainers
All reactions