Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RequestParsingMiddleware for Simplified Request Body Parsing (Fixes #3369) #3426

Conversation

Nirab123456
Copy link

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 the Content-Type header, supporting JSON, form-encoded data, and multipart form data (including file uploads).


Key Changes:

  1. RequestParsingMiddleware:

    • Automatically parses request bodies when the Content-Type is either:
      • application/json
      • application/x-www-form-urlencoded
      • multipart/form-data
    • Sets the parsed data to handler.parsed_body, making it easier to access body data without manual processing.
    • Handles both regular form data and file uploads for multipart requests.
  2. Test Coverage:

    • Added unit tests to verify that 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:

import tornado.ioloop
import tornado.web
from tornado.webmiddleware import RequestParsingMiddleware

class MainHandler(tornado.web.RequestHandler):
    def prepare(self):
        self.parsed_body = None
        self._apply_middlewares()

    def _apply_middlewares(self):
        middlewares = [RequestParsingMiddleware()]
        for middleware in middlewares:
            middleware.process_request(self)

    def post(self):
        self.write(self.parsed_body)

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Issue Fixed:

… and RequestHandler. Enhanced support for JSON, form-encoded, and multipart data, including file uploads. Updated unit tests to cover all scenarios, ensuring robust handling of requests.
… and RequestHandler. Enhanced support for JSON, form-encoded, and multipart data, including file uploads. Updated unit tests to cover all scenarios, ensuring robust handling of requests.
… and RequestHandler. Enhanced support for JSON, form-encoded, and multipart data, including file uploads. Updated unit tests to cover all scenarios, ensuring robust handling of requests.
… and RequestHandler. Enhanced support for JSON, form-encoded, and multipart data, including file uploads. Updated unit tests to cover all scenarios, ensuring robust handling of requests.
… and RequestHandler. Enhanced support for JSON, form-encoded, and multipart data, including file uploads. Updated unit tests to cover all scenarios, ensuring robust handling of requests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tornado.httputil.HTTPServerRequest and tornado.web.RequestHandler
1 participant