bytes(for:delegate:)
Retrieves the contents of a URL based on the specified URL request and delivers an asynchronous sequence of bytes.
Declaration
func bytes(for request: URLRequest, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (URLSession.AsyncBytes, URLResponse)Parameters
- request:
A URL request object that provides request-specific information such as the URL, cache policy, request type, and body data or body stream.
- delegate:
A delegate that receives life cycle and authentication challenge callbacks as the transfer progresses.
Return Value
An asynchronously-delivered tuple that contains a URLSession.AsyncBytes sequence to iterate over, and a URLResponse.
Discussion
Use this method when you want to process the bytes while the transfer is underway. You can use a for-await-in loop to handle each byte. For textual data, use the URLSession.AsyncBytes properties characters, unicodeScalars, or lines to receive the content as asynchronous sequences of those types.
To wait until the session finishes transferring data and receive it in a single Data instance, use data(for:delegate:).