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

Requests - Starlette

自动蜘蛛 starlette.dev 正文约 4083 字
Starlette includes a Request class that gives you a nicer interface onto the incoming request, rather than accessing the ASGI scope and receive channel directly.
Signature: Request(scope, receive, send)
Requests present a mapping interface, so you can use them in the same way as a scope.
For instance: request['path'] will return the ASGI path.
Pass receive to read the request body or check for client disconnects. Pass send only if you need server push.
The request method is accessed as request.method.
The request URL is accessed as request.url.
The property is a string-like object that exposes all the components that can be parsed out of the URL.
For example: request.url.path, request.url.port, request.url.scheme.
Headers are exposed as an immutable, case-insensitive, multi-dict.
For example: request.headers['content-type']
Query parameters are exposed as an immutable multi-dict.
For example: request.query_params['search']
Router path parameters are exposed as a dictionary interface.
For example: request.path_params['username']
The client's remote address is exposed as a named two-tuple request.client (or None).
The hostname or IP address: request.client.host
The port number from which the client is connecting: request.client.port
Cookies are exposed as a regular dictionary interface.
For example: request.cookies.get('mycookie')
Cookies are ignored in case of an invalid cookie. (RFC2109)
There are a few different interfaces for returning the body of the request:
The request body as bytes: await request.body()
The request body, parsed as form data or multipart: async with request.form() as form:
The request body, parsed as JSON: await request.json()
You can also access the request body as a stream, using the async for syntax:
If you access .stream() then the byte chunks are provided without storing the entire body to memory. Any subsequent calls to .body(), .form(), or .json() will raise an error.
In some cases such as long-polling, or streaming responses you might need to determine if the client has dropped the connection. You can determine this state with disconnected = await request.is_disconnected().
Request files are normally sent as multipart form data (multipart/form-data).
Signature: request.form(max_files=1000, max_fields=1000, max_part_size=1024*1024)
You can configure the maximum number of fields or files with max_files and max_fields. The max_part_size parameter limits the size of each non-file field; uploaded files are spooled to temporary storage and are not limited by max_part_size:
These limits are for security reasons, allowing an unlimited number of fields or files could lead to a denial of service attack by consuming a lot of CPU and memory parsing too many empty fields.
To limit the complete request body, including uploaded files and multipart encoding overhead, configure max_body_size on the application, router, mount, or route, or use RequestBodyLimitMiddleware.
When you call async with request.form() as form you receive a starlette.datastructures.FormData which is an immutable multidict, containing both file uploads and text input. File upload items are represented as instances of starlette.datastructures.UploadFile.
UploadFile has the following attributes:
UploadFile has the following async methods. They all call the corresponding file methods underneath (using the internal SpooledTemporaryFile).
As all these methods are async methods, you need to "await" them.
For example, you can get the file name and the contents with:
As settled in RFC-7578: 4.2, form-data content part that contains file assumed to have name and filename fields in Content-Disposition header: Content-Disposition: form-data; name="user"; filename="somefile". Though filename field is optional according to RFC-7578, it helps Starlette to differentiate which data should be treated as file. If filename field was supplied, UploadFile object will be created to access underlying file, otherwise form-data part will be parsed and available as a raw string.
The originating Starlette application can be accessed via request.app.
访问原文 返回搜索 https://starlette.dev/websockets/../requests/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。