Add RequestParsingMiddleware for Simplified Request Body Parsing (Fixes #3369) #3426
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces the
RequestParsingMiddleware
to the Tornado framework, addressing the concerns raised in issue #3369 regarding the need to manually parse POST request bodies. The middleware provides automatic parsing of request bodies based on theContent-Type
header, supporting JSON, form-encoded data, and multipart form data (including file uploads).Key Changes:
RequestParsingMiddleware
:Content-Type
is either:application/json
application/x-www-form-urlencoded
multipart/form-data
handler.parsed_body
, making it easier to access body data without manual processing.Test Coverage:
RequestParsingMiddleware
handles various content types correctly:test_json_parsing
: Verifies JSON body parsing.test_form_parsing
: Validates form-encoded body handling.test_multipart_parsing_with_file
: Tests multipart form data parsing, including file uploads.Example Usage:
This middleware can be easily integrated into Tornado applications. Below is an example of how a handler uses
RequestParsingMiddleware
to automatically parse incoming POST requests:Issue Fixed:
RequestHandler
, especially when dealing with various content types. This also preventsMissingArgumentError
when accessing body arguments directly.