Body
Request+Body.swift:4struct Body
struct Body
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 Request
Represents an HTTP request in an application.
convenience init(application: Application, method: HTTPMethod = .GET, url: URI = "/", version: HTTPVersion = .init(major: 1, minor: 1), headers: HTTPHeaders = .init(), collectedBody: ByteBuffer? = nil, remoteAddress: SocketAddress? = nil, logger: Logger = .init(label: "codes.vapor.request"), byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator(), on eventLoop: EventLoop)
init(application: Application, method: HTTPMethod, url: URI, version: HTTPVersion = .init(major: 1, minor: 1), headersNoUpdate headers: HTTPHeaders = .init(), collectedBody: ByteBuffer? = nil, remoteAddress: SocketAddress? = nil, logger: Logger = .init(label: "codes.vapor.request"), byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator(), on eventLoop: EventLoop)
static func decodeRequest(_ request: Request) -> EventLoopFuture<Request>
static func decodeRequest(_ request: Request) async throws -> Request
let application: Application
var auth: Authentication { get }
Helper for accessing authenticated objects. See Authenticator
for more information.
var body: Body { get }
var byteBufferAllocator: ByteBufferAllocator { get set }
var cache: Cache { get }
var client: Client { get }
var content: ContentContainer { get set }
This container is used to read your Decodable
type using a ContentDecoder
implementation. If no ContentDecoder
is provided, a Request
’s Content-Type
header is used to select a registered decoder.
var cookies: HTTPCookies { get set }
Get and set HTTPCookies
for this Request
This accesses the "Cookie"
header.
var description: String { get }
See CustomStringConvertible
let eventLoop: EventLoop
The EventLoop
which is handling this Request
. The route handler and any relevant middleware are invoked in this event loop.
var fileio: FileIO { get }
var hasSession: Bool { get }
var headers: HTTPHeaders { get set }
The header fields for this HTTP request. The "Content-Length"
and "Transfer-Encoding"
headers will be set automatically when the body
property is mutated.
let id: String
A unique ID for the request.
var logger: Logger { get set }
This Logger from Apple’s swift-log
Package is preferred when logging in the context of handing this Request. Vapor already provides metadata to this logger so that multiple logged messages can be traced back to the same request.
var method: HTTPMethod { get set }
The HTTP method for this request.
var parameters: Parameters { get set }
A container containing the route parameters that were captured when receiving this request. Use this container to grab any non-static parameters from the URL, such as model IDs in a REST API.
var password: Password { get }
var peerAddress: SocketAddress? { get }
We try to determine true peer address if load balancer or reversed proxy provided info in headers
var query: URLQueryContainer { get set }
let remoteAddress: SocketAddress?
The address from which this HTTP request was received by SwiftNIO. This address may not represent the original address of the peer, especially if Vapor receives its requests through a reverse-proxy such as nginx.
var route: Route? { get set }
Route object we found for this request. This holds metadata that can be used for (for example) Metrics.
var serviceContext: ServiceContext { get set }
var services: Services { get }
var session: Session { get }
Returns the current Session
or creates one.
var storage: Storage { get set }
This container is used as arbitrary request-local storage during the request-response lifecycle.Z
var url: URI { get set }
The URL used on this request.
var version: HTTPVersion { get set }
The version for this HTTP request.
var view: ViewRenderer { get }
func redirect(to location: String, redirectType: Redirect = .normal) -> Response
Creates a redirect Response
.
@preconcurrency func webSocket(maxFrameSize: WebSocketMaxFrameSize = .`default`, shouldUpgrade: @escaping ((Request) -> EventLoopFuture<HTTPHeaders?>) = {
$0.eventLoop.makeSucceededFuture([:])
}, onUpgrade: @escaping (Request, WebSocket) -> ()) -> Response
@preconcurrency func webSocket(maxFrameSize: WebSocketMaxFrameSize = .`default`, shouldUpgrade: @escaping ((Request) async throws -> HTTPHeaders?) = { _ in [:] }, onUpgrade: @escaping (Request, WebSocket) async -> ()) -> Response
Upgrades an existing request to a websocket connection
struct Authentication
Request helper for storing and fetching authenticated objects.
struct Password
struct Services
func redirect(to location: String, type: RedirectType) -> Response
Creates a redirect Response
.
protocol AsyncSequence<Element, Failure>
A type that provides asynchronous, sequential, iterated access to its elements.
protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Escapable
protocol Sendable
var data: ByteBuffer? { get }
var description: String { get }
var string: String? { get }
func collect(max: Int? = 1 << 14) -> EventLoopFuture<ByteBuffer?>
@preconcurrency func drain(_ handler: @escaping (BodyStreamResult) -> EventLoopFuture<Void>)
func makeAsyncIterator() -> AsyncIterator
Generates an AsyncIterator
to stream the body’s content as ByteBuffer
sequences. This implementation supports backpressure using NIOAsyncSequenceProducerBackPressureStrategies
struct AsyncIterator
This wrapper generalizes our implementation. RequestBody.AsyncIterator
is the override point for using another implementation
typealias Element = ByteBuffer
func allSatisfy(_ predicate: (Self.Element) async throws -> Bool) async rethrows -> Bool
Returns a Boolean value that indicates whether all elements produced by the asynchronous sequence satisfy the given predicate.
func collect(upTo maxBytes: Int) async throws -> ByteBuffer
Accumulates an AsyncSequence
of ByteBuffer
s into a single ByteBuffer
.
func collect(upTo maxBytes: Int, into accumulationBuffer: inout ByteBuffer) async throws
Accumulates an AsyncSequence
of ByteBuffer
s into a single accumulationBuffer
.
@preconcurrency func compactMap<ElementOfResult>(_ transform: @escaping (Self.Element) async -> ElementOfResult?) -> AsyncCompactMapSequence<Self, ElementOfResult>
Creates an asynchronous sequence that maps the given closure over the asynchronous sequence’s elements, omitting results that don’t return a value.
@preconcurrency func compactMap<ElementOfResult>(_ transform: @escaping (Self.Element) async throws -> ElementOfResult?) -> AsyncThrowingCompactMapSequence<Self, ElementOfResult>
Creates an asynchronous sequence that maps an error-throwing closure over the base sequence’s elements, omitting results that don’t return a value.
func contains(_ search: Self.Element) async rethrows -> Bool
Returns a Boolean value that indicates whether the asynchronous sequence contains the given element.
func contains(where predicate: (Self.Element) async throws -> Bool) async rethrows -> Bool
Returns a Boolean value that indicates whether the asynchronous sequence contains an element that satisfies the given predicate.
@preconcurrency func drop(while predicate: @escaping (Self.Element) async -> Bool) -> AsyncDropWhileSequence<Self>
Omits elements from the base asynchronous sequence until a given closure returns false, after which it passes through all remaining elements.
func dropFirst(_ count: Int = 1) -> AsyncDropFirstSequence<Self>
Omits a specified number of elements from the base asynchronous sequence, then passes through all remaining elements.
@preconcurrency func filter(_ isIncluded: @escaping (Self.Element) async -> Bool) -> AsyncFilterSequence<Self>
Creates an asynchronous sequence that contains, in order, the elements of the base sequence that satisfy the given predicate.
func first(where predicate: (Self.Element) async throws -> Bool) async rethrows -> Self.Element?
Returns the first element of the sequence that satisfies the given predicate.
@preconcurrency func flatMap<SegmentOfResult>(_ transform: @escaping (Self.Element) async throws -> SegmentOfResult) -> AsyncThrowingFlatMapSequence<Self, SegmentOfResult> where SegmentOfResult : AsyncSequence
Creates an asynchronous sequence that concatenates the results of calling the given error-throwing transformation with each element of this sequence.
@preconcurrency func flatMap<SegmentOfResult>(_ transform: @escaping (Self.Element) async -> SegmentOfResult) -> AsyncFlatMapSequence<Self, SegmentOfResult> where SegmentOfResult : AsyncSequence, Self.Failure == SegmentOfResult.Failure
Creates an asynchronous sequence that concatenates the results of calling the given transformation with each element of this sequence.
@preconcurrency func flatMap<SegmentOfResult>(_ transform: @escaping (Self.Element) async -> SegmentOfResult) -> AsyncFlatMapSequence<Self, SegmentOfResult> where SegmentOfResult : AsyncSequence, SegmentOfResult.Failure == Never
Creates an asynchronous sequence that concatenates the results of calling the given transformation with each element of this sequence.
@preconcurrency func flatMap<SegmentOfResult>(_ transform: @escaping (Self.Element) async -> SegmentOfResult) -> AsyncFlatMapSequence<Self, SegmentOfResult> where SegmentOfResult : AsyncSequence, Self.Failure == Never, SegmentOfResult.Failure == Never
Creates an asynchronous sequence that concatenates the results of calling the given transformation with each element of this sequence.
@preconcurrency func map<Transformed>(_ transform: @escaping (Self.Element) async -> Transformed) -> AsyncMapSequence<Self, Transformed>
Creates an asynchronous sequence that maps the given closure over the asynchronous sequence’s elements.
@preconcurrency func map<Transformed>(_ transform: @escaping (Self.Element) async throws -> Transformed) -> AsyncThrowingMapSequence<Self, Transformed>
Creates an asynchronous sequence that maps the given error-throwing closure over the asynchronous sequence’s elements.
@warn_unqualified_access func max(by areInIncreasingOrder: (Self.Element, Self.Element) async throws -> Bool) async rethrows -> Self.Element?
Returns the maximum element in the asynchronous sequence, using the given predicate as the comparison between elements.
@warn_unqualified_access func min(by areInIncreasingOrder: (Self.Element, Self.Element) async throws -> Bool) async rethrows -> Self.Element?
Returns the minimum element in the asynchronous sequence, using the given predicate as the comparison between elements.
func prefix(_ count: Int) -> AsyncPrefixSequence<Self>
Returns an asynchronous sequence, up to the specified maximum length, containing the initial elements of the base asynchronous sequence.
@preconcurrency func prefix(while predicate: @escaping (Self.Element) async -> Bool) rethrows -> AsyncPrefixWhileSequence<Self>
Returns an asynchronous sequence, containing the initial, consecutive elements of the base sequence that satisfy the given predicate.
func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Self.Element) async throws -> Result) async rethrows -> Result
Returns the result of combining the elements of the asynchronous sequence using the given closure.
func reduce<Result>(into initialResult: Result, _ updateAccumulatingResult: (inout Result, Self.Element) async throws -> Void) async rethrows -> Result
Returns the result of combining the elements of the asynchronous sequence using the given closure, given a mutable initial value.