Body
Represents a Response
’s body.
struct Body
let body = Response.Body(string: "Hello, world!")
This can contain any data (streaming or static) and should match the message’s "Content-Type"
header.
Represents a Response
’s body.
struct Body
let body = Response.Body(string: "Hello, world!")
This can contain any data (streaming or static) and should match the message’s "Content-Type"
header.
import Vapor
Vapor is a framework for building server applications, APIs and websites in Swift. It provides a safe, performant and scalable foundation for building large complex backends.
final class Response
An HTTP response from a server back to the client.
convenience init(status: HTTPResponseStatus = .ok, version: HTTPVersion = .init(major: 1, minor: 1), headers: HTTPHeaders = .init(), body: Body = .empty)
Creates a new Response
.
init(status: HTTPResponseStatus, version: HTTPVersion, headersNoUpdate headers: HTTPHeaders, body: Body)
Internal init that creates a new Response
without sanitizing headers.
var body: Body { get set }
The Body
. Updating this property will also update the associated transport headers.
var content: ContentContainer { get set }
var cookies: HTTPCookies { get set }
Get and set HTTPCookies
for this Response
. This accesses the "Set-Cookie"
header.
var description: String { get }
See CustomStringConvertible
var headers: HTTPHeaders { get set }
The header fields for this HTTP response. The "Content-Length"
and "Transfer-Encoding"
headers will be set automatically when the body
property is mutated.
var status: HTTPResponseStatus { get set }
The HTTP response status.
var storage: Storage { get set }
var upgrader: Upgrader? { get set }
Optional Upgrade behavior to apply to this response. currently, websocket upgrades are the only defined case.
var version: HTTPVersion { get set }
The HTTP version that corresponds to this response.
func encodeResponse(for request: Request) -> EventLoopFuture<Response>
func encodeResponse(for request: Request) async throws -> Response
protocol CustomStringConvertible
A type with a customized textual representation.
protocol ExpressibleByExtendedGraphemeClusterLiteral : ExpressibleByUnicodeScalarLiteral
A type that can be initialized with a string literal containing a single extended grapheme cluster.
protocol ExpressibleByStringLiteral : ExpressibleByExtendedGraphemeClusterLiteral
A type that can be initialized with a string literal.
protocol ExpressibleByUnicodeScalarLiteral
A type that can be initialized with a string literal containing a single Unicode scalar value.
protocol Sendable
init(asyncStream: @escaping (AsyncBodyStreamWriter) async throws -> (), byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Creates a chunked HTTP Response
steam using AsyncBodyStreamWriter
.
init(asyncStream: @escaping (AsyncBodyStreamWriter) async throws -> (), count: Int, byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Creates a chunked HTTP Response
steam using AsyncBodyStreamWriter
.
init(buffer: ByteBuffer, byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Create a new body from a Swift NIO ByteBuffer
.
init(byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Creates an empty body. Useful for GET
requests where HTTP bodies are forbidden.
init(data: Data, byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Create a new body wrapping Data
.
init(dispatchData: DispatchData, byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Create a new body wrapping DispatchData
.
init(managedAsyncStream: @escaping (AsyncBodyStreamWriter) async throws -> (), byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Creates a managed chunked HTTP Response
steam using AsyncBodyStreamWriter
that automtically closes or fails based if the closure throws an error or returns.
init(managedAsyncStream: @escaping (AsyncBodyStreamWriter) async throws -> (), count: Int, byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Creates a managed chunked HTTP Response
steam using AsyncBodyStreamWriter
that automtically closes or fails based if the closure throws an error or returns.
init(staticString: StaticString, byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Create a new body from the UTF8 representation of a StaticString
.
@preconcurrency init(stream: @escaping (BodyStreamWriter) -> (), byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
@preconcurrency init(stream: @escaping (BodyStreamWriter) -> (), count: Int, byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
init(string: String, byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator())
Create a new body from the UTF8 representation of a String
.
init(stringLiteral value: String)
ExpressibleByStringLiteral
conformance.
static let empty: Body
An empty Response.Body
.
var buffer: ByteBuffer? { get }
var count: Int { get }
The size of the HTTP body’s data. -1
is a chunked stream.
var data: Data? { get }
Returns static data if not streaming.
var description: String { get }
See CustomDebugStringConvertible
.
var string: String? { get }
func collect(on eventLoop: EventLoop) -> EventLoopFuture<ByteBuffer?>
init(extendedGraphemeClusterLiteral value: Self.StringLiteralType)
init(unicodeScalarLiteral value: Self.ExtendedGraphemeClusterLiteralType)