response - an HTTP response wrapper

responsify smth

Convert some object into a Response. Supported objects are:

This function is a coroutine.

jsonify smth

Encode a JSON object with json.dumps, return a Response with an appropriate Content-Type set.

Abort code description headers

An exception that signals HTTP errors. description must be a string; headers has to be something that can be converted into a dict.

code

description

headers

Now as a struct.Headers.

name

What this error code means according to the HTTP standard.

Response code headers data

A single-chunk response. data must be a bytes object, headers should be convertable into a dict.

code

description

data

A list of chunks.

headers

Now as a struct.Headers.

defheaders

A property that returns some headers. These will be sent to the client unless overriden in the constructor. By default, these are:

communicate reader writer

Called after the response is sent to the client. reader is an asyncio.StreamReader the request was received from, writer is an asyncio.StreamWriter the response was sent through. Useful for upgrading to other protocols (e.g. HTTP 2 or WebSocket.)

This method is a coroutine.

FileResponse code path headers bufsize

A response that sends data from a local file instead of a string. Inherits Response. If the file does not exist or is not readable, constructor throws HTTP 404.

path

bufsize

defheaders

Same as Response.defheaders, except Content-Length is determined via os.stat.

data

A property that returns an iterator of chunks, then closes the file after yielding the last one.

abort code info headers

Raise Abort. Accepts the same arguments.

redirect path info code headers

Issue a redirect to another path by raising Abort with a Location header. By default, code is 302 (Found).

static path attachment headers

Guess the MIME type from the file name, create a FileResponse. If attachment is true, Content-Disposition is set to attachment (meaning browsers will download the file instead of attempting to display it.)

NOTE: this function will happily send any file; normalize the filename first if you’ve received it from an unknown source! For example, a client can easily request /static/../../../../../../../etc/passwd; to avoid this, call os.path.normpath before os.path.join.