Request and response objects

Overview

All handlers and middleware receive an instance of the Request class and must return an instance of the Response class.

This document explains the APIs for Response and Request.

Request objects

class pyroutes.http.request.Request

Attributes

Request.GET

A dictionary of the given GET variables.

Request.POST

A dictionary of the given POST variables. If a multiple key-value pairs has the same key name, the dictionary value for the key will be a list of the values.

Request.FILES

A dictionary of the given files. As with the POST variables, multiple keys with the same name will result in a list. The values are tuples consisting of the filename and a file-like object.

Request.COOKIES

An instance if RequestCookieHandler which provides the following API.

Return the value of a signed cookie, or None if it doesn’t exist. Raises CookieHashInvalid or CookieHashMissing if the user has altered the cookies in any way.

Return the value of a cookie without validating it’s authenticity. Returns None if the cookie doesn’t exist.

Request.ENV

The environment as passed from WSGI.

Response objects

Response([content=None, headers=None, status_code='200 OK',
default_content_header=True])

The base response class. Constructor initializes the attributes with its given values. If default_content_header is true, and Content-Type is not passed in headers, the content type defined in pyroutes.settings.DEFAULT_CONTENT_TYPE will be added to the headers automatically.

The status code can be passed as either a full status code or an integer corresponding to a standard code.

Attributes

Response.content

A string or an iterable object that is passed to the browser.

Response.status_code

The HTTP status code sent to the client. Can be either a full string representation of the status code, or just the number id.

Response.headers

A list of tuples with key-value pairs of headers and their value.

Response.cookies

An instance of ResponseCookieHandler which provides the following API.

Adds a signed cookie to the response. The expires parameter must be an instance of datetime.datetime and set the cookie expiration to its value. Defaults to infinite lifetime.

Same functionality as add_cookie only the cookie will not be signed, and is not tamper proof.

Deletes a cookie from the browser.

Redirect objects

class pyroutes.http.response.Redirect(location[, absolute_path=False])

A redirect shortcut class for redirection responses. This class can make two types of redirects:

  1. Absolute path redirects: When you want do redirect to outside your application.
  2. Root App relative redirect: If you want your redirection relative to the root application path.
  3. Site relative redirect: If you want your redirection relative to the base of the domain.
  4. Current URL relative redirect: If you want your redirection relative to the current URL value.

Example of these uses, assuming your app is installed on http://example.com/apps/myapp:

  1. Redirect('http://pyroutes.com/') for a redirect to pyroutes.com.
  2. Redirect('/some/path') for a redirect to http://example.com/apps/myapp/some/path. Ignores current URL.
  3. Redirect('/other/path', absolute_path=True) to redirect to http://example.com/other/path. Ignores current URL.
  4. Redirect('relative/path') to redirect to a path relative to the current URL of the user’s browser. E.g. if the user is visiting http://example.com/apps/myapp/foo/ this redirect would go to http://example.com/apps/myapp/foo/relative/path

HttpException objects

class pyroutes.http.response.HttpException(location[, **template_data])

Base class for all exceptions that produce special error pages. If instances of objects that inherited from HttpException are raised, the ErrorHandlerMiddleware will render a page and return it with the correct HTTP code. The base template can be overriden using settings.CUSTOM_BASE_TEMPLATE and additional template data kan be passed to the exception. Raising a HttpException without passing a location as first parameter is allowed, location is then populated from the PATH_INFO variable.

class pyroutes.http.response.Http403

Template for this HttpException can be overridden using settings.TEMPLATE_403

class pyroutes.http.response.Http404

Template for this HttpException can be overridden using settings.TEMPLATE_404

class pyroutes.http.response.Http500

Template for this HttpException can be overridden using settings.TEMPLATE_500

Project Versions

Go to this page on GitHub. (Edit)

Table Of Contents

Previous topic

Route handling

Next topic

Templates

This Page