网页 综合 更新时间 2026-11-14 09:44:57

Responses - Starlette

自动蜘蛛 starlette.dev 正文约 3075 字
Starlette includes a few response classes that handle sending back the appropriate ASGI messages on the send channel.
Signature: Response(content, status_code=200, headers=None, media_type=None)
Starlette will automatically include a Content-Length header. It will also include a Content-Type header, based on the media_type and appending a charset for text types, unless a charset has already been specified in the media_type.
Once you've instantiated a response, you can send it by calling it as an ASGI application instance.
Starlette provides a set_cookie method to allow you to set cookies on the response object.
Signature: Response.set_cookie(key, value, max_age=None, expires=None, path="/", domain=None, secure=False, httponly=False, samesite="lax", partitioned=False)
Conversely, Starlette also provides a delete_cookie method to manually expire a set cookie.
Signature: Response.delete_cookie(key, path='/', domain=None, secure=False, httponly=False, samesite="lax", partitioned=False)
To delete a partitioned cookie on Python 3.14+:
Pass partitioned=True to expire the cookie in the current partition. It defaults to False. Use the same path and domain as the original cookie, within the same top-level site context. For cross-site requests, use samesite="none".
Partitioned cookies require Secure
Pass secure=True when deleting a partitioned cookie over HTTPS. Browsers reject the expiration header if it includes Partitioned without Secure.
Takes some text or bytes and returns an HTML response.
Takes some text or bytes and returns a plain text response.
Takes some data and returns an application/json encoded response.
If you need fine-grained control over JSON serialization, you can subclass JSONResponse and override the render method.
For example, if you wanted to use a third-party JSON library such as orjson:
In general you probably want to stick with JSONResponse by default unless you are micro-optimising a particular endpoint or need to serialize non-standard object types.
Returns an HTTP redirect. Uses a 307 status code by default.
Takes an async generator or a normal generator/iterator and streams the response body.
Have in mind that file-like objects (like those created by open()) are normal iterators. So, you can return them directly in a StreamingResponse.
Asynchronously streams a file as the response.
Takes a different set of arguments to instantiate than the other response types:
File responses will include appropriate Content-Length, Last-Modified and ETag headers.
File responses also supports HTTP range requests.
The Accept-Ranges: bytes header will be included in the response if the file exists. For now, only the bytes range unit is supported.
If the request includes a Range header, and the file exists, the response will be a 206 Partial Content response with the requested range of bytes. If the range is invalid, the response will be a 416 Range Not Satisfiable response.
A response class that implements Server-Sent Events. It enables event streaming from the server to the client without the complexity of websockets.
访问原文 返回搜索 https://starlette.dev/middleware/../responses/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。