Structure
Response.Body
Represents a
Response’s body.struct Body
Overview
let body = Response.Body(string: "Hello, world!")
This can contain any data (streaming or static) and should match the message’s "Content-Type" header.
Topics
Structures
struct AlreadyConsumedErrorThrown when a streaming body is read a second time after being streamed away.struct NegativeCountErrorThrown when a streaming body is created with a negative length.
Initializers
init()Creates an empty body. Useful forGETrequests where HTTP bodies are forbidden.init(data: Data)Create a new body wrappingData.init(staticString: StaticString)Create a new body from the UTF8 representation of aStaticString.init(stream: @Sendable (borrowing any HTTPBodyWriter & ~Escapable) async throws -> Void)Creates a chunked, streaming HTTPResponsebody of unknown length.init(stream: @Sendable (borrowing any HTTPBodyWriter & ~Escapable) async throws -> Void, count: Int?) throwsCreates a chunked, streaming HTTPResponsebody.init(string: String)Create a new body from the UTF8 representation of aString.init(stringLiteral: String)ExpressibleByStringLiteralconformance.
Instance Properties
var count: Int?The size of the HTTP body’s data, ornilif it is not known.var data: Data?The body’s bytes, ornilif it is empty or is a stream nothing has collected.var description: Stringvar string: String?The body decoded as UTF-8, ornilif it is empty or is a stream nothing has collected.
Instance Methods
func collect(max: BodySizeLimit) async throws -> Data?Reads the whole body into memory, running a streaming body to completion.func data(max: BodySizeLimit) async throws -> Data?The body’s bytes, collecting a streaming body first if nothing has collected it yet.func reduceBytes<R>(into: R, (inout R, Span<UInt8>) async throws -> Void) async throws -> RFold the body’s bytes into a value, one chunk at a time.func string(max: BodySizeLimit) async throws -> String?The body decoded as UTF-8, collecting a streaming body first if nothing has collected it yet.func withStreamingBytes((Span<UInt8>) async throws -> Void) async throwsRead the body’s bytes incrementally, without buffering the whole thing.
Type Properties
static let empty: BodyAn emptyResponse.Body.
Relationships
Conforms To
Swift.CustomStringConvertibleSwift.ExpressibleByExtendedGraphemeClusterLiteralSwift.ExpressibleByStringLiteralSwift.ExpressibleByUnicodeScalarLiteralSwift.SendableSwift.SendableMetatype