Structure
Request.Body
A view onto this request’s body.
struct Body
Overview
Non-escapable, and bound to the Request it was read from: it can be used for as long as that request value is in scope, but it cannot be stored in a property, returned without a lifetime, or captured by an escaping closure, a Task, or an async let. Capture the Request instead and take .body from it where it is needed.
Topics
Instance Properties
var data: Data?The buffered body, ornilif there is none or nothing has collected it yet.var description: StringThe buffered body as UTF-8, or empty if nothing has collected it. NotCustomStringConvertible, because that protocol requires an escapable conformer.var string: String?The buffered body as UTF-8, ornilif there is none or nothing has collected it yet.
Instance Methods
func collect(max: BodySizeLimit) async throws -> Data?Buffers the body into memory, aborting with 413 if it exceeds the ceiling.func data(max: BodySizeLimit) async throws -> Data?The body’s bytes, collecting the stream first if nothing has collected it yet.func forEachChunk((Span<UInt8>) async throws -> Void) async throwsDrives the read loop for you, callingbodywith each chunk of the request body. This is what most streaming handlers want; it is a convenience overwithReader(_:)and shares its semantics withforEachChunk(_:)(borrowed span, valid only for the call — copy out anything you keep).func string(max: BodySizeLimit) async throws -> String?The body decoded as UTF-8, collecting the stream first if nothing has collected it yet.func withReader<R>((borrowing any RequestBodyReader & ~Escapable) async throws -> R) async throws -> RLends aRequestBodyReaderfor the duration ofbody, so the handler can drive the read loop itself (read a single chunk, interleave reads with other work). The reader is non-escapable: using it after the request moved on is a compile-time error.
Relationships
Conforms To
Swift.SendableSwift.SendableMetatype