request - a neater representation of HTTP requests

Request

A single HTTP request. Instances of this class are created by app automatically; you don’t need to do that yourself.

version

The HTTP version the client used as a (major, minor) tuple.

method

The HTTP method used by the client, e.g. GET or POST.

path

The path requested by the client. Relative to the current routing module (see route.dir.)

headers

Additional HTTP headers as a struct.Headers.

payload

The request body as a bytes object.

app

The instance of the application that handled this requests, i.e. the thing created with app.

handler

The top-level handler this request was passed to (the one you created an instance of app with.)

loop

The asyncio event loop everything is running in.

log

A logging.Logger named dogeweb.

accept

Parsed values of various Accept headers.

All of these are represented by struct.Accept objects. MIME types, charsets, or whatever not specified in the request will have a quality of 0.

cookies

All cookies from this request as a semi-immutable dict.

set name value maxage path httponly

Create a new cookie. Note that you still have to create a Response with the appropriate header; this method returns a singleton list containing that header.

handler = req ->
  headers  = req.cookies.set 'visited' 'true'
  headers += req.cookies.set 'last_ts' $ str time!
  # ...
  200, headers, more_headers, ..., '<!doctype html><html>...</html>'

By default, a cookie is module-local, i.e. path-restricted to the current routing module and its submodules (see route.dir). Override path if that’s not desired.

headers  = req.cookies.set 'visited_module' 'yes'
headers += req.cookies.set 'visited_site' 'true' path: '/'

maxage is the expiration time in seconds; 0 (the default) means the cookie is valid until the browser window is closed.

When reading cookies, local ones have priority over global ones. If the above code used the same name for both cookies, requests to the same module would always contain yes while requests to other modules would have that cookie set to true.

del name path

Remove a cookie (by setting its max-age to 0.) The path should match the one used when creating that cookie exactly. By default, this removes a cookie belonging to the current module. Just like set, returns a singleton list with a header to pass to the client.

del is a reserved name in Python, so there’s an alias available, rem.

args

Form fields passed through the URL (the part after ?) as a struct.MultiDict.

form

Form fields passed through the request body. This includes files, too.

files

Same as form, but only top-level files are included. Removes the need for some type-checking.

isxhr

Whether this request was made through XmlHttpRequest (aka AJAX).

isjson

Whether the client expects JSON instead of HTML. Useful for building APIs.

address

The IP address of the client as a string.

fullpath

The requested path. This one is absolute, unlike path, which is relative to the module.

parent

The difference between fullpath and `path.

url_for __name args

Given the name of a route (in a syntax similar to Python relative imports) and some keyword arguments, construct an URL that points to that route.

For example, given this route structure:

/hello/    -> modhello (module)
  /world/    -> world
  /user/     -> user
/          -> main (module)
  /          -> root
  /login/    -> login
  /u/<name>/ -> userinfo

Suppose the user has requested /hello/user/, meaning the current route’s path is modhello.user and the currently used module is modhello. Here’s what different calls to url_for would return:

url_for '.world'                     -> '/hello/world/'
url_for '..modhello.user'            -> '/hello/user/'
url_for '..main.root'                -> '/'
url_for 'main.login'                 -> '/login/'
url_for 'main.userinfo' name: 'spam' -> '/u/spam/'

The following methods do not use any info from the request; they’re shortcuts.

abort code info headers

Same as response.abort.

redirect path info code

Same as response.redirect.

static path attachment headers

Same as response.static.

jsonify smth

Same as response.jsonify.