Contents

data(for:delegate:)

Downloads the contents of a URL based on the specified URL request and delivers the data asynchronously.

Declaration

func data(for request: URLRequest, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (Data, 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 the URL contents as a Data instance, and a URLResponse.

Discussion

Use this method to wait until the session finishes transferring data and receive it in a single Data instance. To process the bytes as the session receives them, use bytes(for:delegate:).

See Also

Performing asynchronous transfers