base

class litestar.response.base.ASGIResponse[源代码]

基类:object

A low-level ASGI response class.

__init__(*, background: BackgroundTask | BackgroundTasks | None = None, body: bytes | str = b'', content_length: int | None = None, cookies: Iterable[Cookie] | None = None, encoding: str = 'utf-8', headers: dict[str, Any] | Iterable[tuple[str, str]] | None = None, is_head_response: bool = False, media_type: MediaType | str | None = None, status_code: int | None = None) None[源代码]

A low-level ASGI response class.

参数:
  • background -- A background task or a list of background tasks to be executed after the response is sent.

  • body -- encoded content to send in the response body.

  • content_length -- The response content length.

  • cookies -- The response cookies.

  • encoding -- The response encoding.

  • headers -- The response headers.

  • is_head_response -- A boolean indicating if the response is a HEAD response.

  • media_type -- The response media type.

  • status_code -- The response status code.

encode_headers() list[tuple[bytes, bytes]][源代码]

Return a list of headers for this response.

The list contains both headers and encoded cookies, as tuples, where each tuple represents a single header key-value pair encoded as bytes.

async after_response() None[源代码]

Execute after the response is sent.

返回:

None

async start_response(send: Send) None[源代码]

Emit the start event of the response. This event includes the headers and status codes.

参数:

send -- The ASGI send function.

返回:

None

async send_body(send: Send, receive: Receive) None[源代码]

Emit the response body.

参数:
  • send -- The ASGI send function.

  • receive -- The ASGI receive function.

备注

  • Response subclasses should customize this method if there is a need to customize sending data.

返回:

None

async __call__(scope: Scope, receive: Receive, send: Send) None[源代码]

ASGI callable of the Response.

参数:
  • scope -- The ASGI connection scope.

  • receive -- The ASGI receive function.

  • send -- The ASGI send function.

返回:

None

class litestar.response.base.Response[源代码]

基类:Generic[T]

Base Litestar HTTP response class, used as the basis for all other response classes.

__init__(content: T, *, background: BackgroundTask | BackgroundTasks | None = None, cookies: ResponseCookies | None = None, encoding: str = 'utf-8', headers: ResponseHeaders | None = None, media_type: MediaType | OpenAPIMediaType | str | None = None, status_code: int | None = None, type_encoders: TypeEncodersMap | None = None) None[源代码]

Initialize the response.

参数:
  • content -- A value for the response body that will be rendered into bytes string.

  • status_code -- An HTTP status code.

  • media_type -- A value for the response Content-Type header.

  • background -- A BackgroundTask instance or BackgroundTasks to execute after the response is finished. Defaults to None.

  • headers -- A string keyed dictionary of response headers. Header keys are insensitive.

  • cookies -- A list of Cookie instances to be set under the response Set-Cookie header.

  • encoding -- The encoding to be used for the response headers.

  • type_encoders -- A mapping of types to callables that transform them into types supported for serialization.

set_cookie(key: str, value: str | None = None, max_age: int | None = None, expires: int | None = None, path: str = '/', domain: str | None = None, secure: bool = False, httponly: bool = False, samesite: Literal['lax', 'strict', 'none'] = 'lax') None

Set a cookie on the response. If passed a Cookie instance, keyword arguments will be ignored.

参数:
  • key -- Key for the cookie or a Cookie instance.

  • value -- Value for the cookie, if none given defaults to empty string.

  • max_age -- Maximal age of the cookie before its invalidated.

  • expires -- Seconds from now until the cookie expires.

  • path -- Path fragment that must exist in the request url for the cookie to be valid. Defaults to /.

  • domain -- Domain for which the cookie is valid.

  • secure -- Https is required for the cookie.

  • httponly -- Forbids javascript to access the cookie via document.cookie.

  • samesite -- Controls whether a cookie is sent with cross-site requests. Defaults to lax.

返回:

None.

set_header(key: str, value: Any) None[源代码]

Set a header on the response.

参数:
  • key -- Header key.

  • value -- Header value.

返回:

None.

set_etag(etag: str | ETag) None[源代码]

Set an etag header.

参数:

etag -- An etag value.

返回:

None

Delete a cookie.

参数:
  • key -- Key of the cookie.

  • path -- Path of the cookie.

  • domain -- Domain of the cookie.

返回:

None.

render(content: Any, media_type: str, enc_hook: Serializer = <function default_serializer>) bytes[源代码]

Handle the rendering of content into a bytes string.

返回:

An encoded bytes string

to_asgi_response(request: Request, *, background: BackgroundTask | BackgroundTasks | None = None, cookies: Iterable[Cookie] | None = None, headers: dict[str, str] | None = None, is_head_response: bool = False, media_type: MediaType | str | None = None, status_code: int | None = None, type_encoders: TypeEncodersMap | None = None) ASGIResponse[源代码]

Create an ASGIResponse from a Response instance.

参数:
  • background -- Background task(s) to be executed after the response is sent.

  • cookies -- A list of cookies to be set on the response.

  • headers -- Additional headers to be merged with the response headers. Response headers take precedence.

  • is_head_response -- Whether the response is a HEAD response.

  • media_type -- Media type for the response. If media_type is already set on the response, this is ignored.

  • request -- The Request instance.

  • status_code -- Status code for the response. If status_code is already set on the response, this is

  • type_encoders -- A dictionary of type encoders to use for encoding the response content.

返回:

An ASGIResponse instance.