Skip to main content

Understanding Tornado RequestHandler

Introduction

In the world of Tornado, the RequestHandler is the go-to class for handling HTTP requests. It is the base class for all request handlers, making it an essential piece of the Tornado framework.

What is RequestHandler?

RequestHandler is the primary building block for your Tornado web application. It is responsible for handling requests, formulating responses, and essentially managing the flow of web transactions. Each HTTP request that your application receives results in the instantiation of a new RequestHandler.

How to Use RequestHandler

Let's get started with a simple example.

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")

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

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

In this code, we've defined a MainHandler class that inherits from RequestHandler. When a GET request is made to our server, it calls the get method in our MainHandler.

Overriding HTTP method handlers

Tornado's RequestHandler is designed to be flexible and can handle any type of HTTP request. To do this, you can override the methods corresponding to the HTTP verbs (GET, POST, PUT, DELETE, HEAD, OPTIONS) in your RequestHandler subclass.

class MainHandler(tornado.web.RequestHandler):
def get(self):
# handle GET request here
pass

def post(self):
# handle POST request here
pass

Accessing Request Information

RequestHandler provides several properties and methods to access information about the request:

  • self.request: This is the HTTPRequest object that represents the client's request.
  • self.request.uri: This property gets the full request URI.
  • self.request.headers: This property gets the HTTP headers.
  • self.get_query_argument(name): This method retrieves the specified query string argument.

Writing the Response

RequestHandler provides methods for writing the HTTP response:

  • self.write(chunk): This method writes the given chunk to the output buffer.
  • self.set_header(name, value): This method sets the given response header name and value.
  • self.set_status(status_code): This method sets the status code for our response.
  • self.finish(): This method finishes the HTTP request and ends the HTTP response.

Handling Errors

If an error occurs while the request is being processed, you can send an error response using the send_error method. You can also customize the error page by overriding the write_error method.

class ErrorHandler(tornado.web.RequestHandler):
def write_error(self, status_code, **kwargs):
self.write("Gosh darnit, user! You caused a %d error." % status_code)

Conclusion

The RequestHandler is the backbone of how Tornado handles HTTP requests and responses. It provides a flexible and straightforward interface for processing HTTP methods, accessing request information, writing responses, and handling errors. By mastering the use of RequestHandler, you can effectively manage the flow of web transactions in your Tornado application.