tornadoweb / tornado

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.
http://www.tornadoweb.org/
Apache License 2.0
21.75k stars 5.5k forks source link

tornado.httputil.HTTPServerRequest and tornado.web.RequestHandler #3369

Open Stronking opened 7 months ago

Stronking commented 7 months ago

In the tornado.httputil.HTTPServerRequest class, why is the body_arguments attribute initialized as an empty dictionary {}? This design choice necessitates developers to manually parse the request.body when handling POST request bodies, and it precludes the use of parameter assertions. Consequently, when utilizing the tornado.web.RequestHandler.get_body_argument[s]() methods, a MissingArgumentError is consistently thrown if the corresponding parameter is missing from the request body. Will this issue be addressed or improved in future versions?  This problem can hinder user experience and code robustness, especially for developers who are new to the Tornado framework. It may increase the difficulty of understanding and using the framework. It would be appreciated if the development team could consider optimizing this issue or providing clearer documentation on how to handle it.

bdarnell commented 5 months ago

why is the body_arguments attribute initialized as an empty dictionary {}?

What alternative do you have in mind?

This design choice necessitates developers to manually parse the request.body when handling POST request bodies,

Ah, are you talking about parsing JSON or other body formats? The (query|body)_argument(s) attributes and methods are specific to HTML forms, and they inherit a lot of quirks from the limitations of the form-encoded format. If you're using something other than HTML form-encoded data, just ignore all of this and do something like self.json_args = parse(self.request.body) in an override of prepare().

Will this issue be addressed or improved in future versions?

I have no plans for changes here. I have vague interest in some sort of middleware scheme that would let you centralize request parsing but no concrete plans.