HTTP Routing utilities

The pelix.http.routing module provides a utility class and a set of decorators to ease the development of REST-like servlets.



A servlet which uses the utility decorators must inherit from the pelix.http.routing.RestDispatcher class.

The pelix.http.routing.RestDispatcher class handles all do_* methods and calls the corresponding decorated methods in the child class.

The child class can declare as many methods as necessary, with any name (public, protected or private) and decorate them with the following decorators. Note that a method can be decorated multiple times.

The decorated methods muse have the following signature:

decorated_method(request, response, **kwargs)

Called by the dispatcher to handle a request.

The keyword arguments must have the same name as the ones given in the URL pattern in the decorators.

  • request – An AbstractHTTPServletRequest object
  • response – An AbstractHTTPServletResponse object

Supported types

Each argument in the URL can be automatically converted to the requested type. If the conversion fails, an error 500 is automatically sent back to the client.

Type Description
string Simple string used as is. The string can’t contain a slash (/)
int The argument is converted to an integer. The input must be of base 10. Floats are rejected.
float The argument is converted to a float. The input must be of base 10.
path A string representing a path, containing slashes.
uuid The argument is converted to a uuid.UUID class.

Multiple arguments can be given at a time, but can only be of one type.


from pelix.ipopo.decorators import ComponentFactory, Provides, Property, \
from pelix.http import HTTP_SERVLET, HTTP_SERVLET_PATH
from pelix.http.routing import RestDispatcher, HttpGet, HttpPost, HttpPut

@Property('_path', HTTP_SERVLET_PATH, '/api/v0')
class SomeServlet(RestDispatcher):
   def list_elements(self, request, response):
      response.send_content(200, "<p>The list</p>")

   def handle_form(self, request, response, form_id):
      response.send_content(200, "<p>Handled {}</p>".format(form_id))

   def handle_upload(
   self, request, response,
                     some_id=None, filename=None):
      response.send_content(200, "<p>Handled {} : {}</p>" \
         .format(some_id, filename))