<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.9.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSessionDataDelegate/urlSession(_:dataTask:didReceive:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLSessionDataDelegate(im)URLSession:dataTask:didReceiveResponse:completionHandler:"
  },
  "title" : "urlSession(_:dataTask:didReceive:completionHandler:)"
}
-->

# urlSession(_:dataTask:didReceive:completionHandler:)

Tells the delegate that the data task received the initial reply (headers) from the server.

```
optional func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping @Sendable (URLSession.ResponseDisposition) -> Void)
```

## Parameters

`session`

The session containing the data task that received an initial reply.

`dataTask`

The data task that received an initial reply.

`response`

A URL response object populated with headers.

`completionHandler`

A completion handler that your code calls to continue a transfer, passing a [`URLSession.ResponseDisposition`](/documentation/Foundation/URLSession/ResponseDisposition) constant to indicate whether the transfer should continue as a data task or should become a download task.

- If you pass [`URLSession.ResponseDisposition.allow`](/documentation/Foundation/URLSession/ResponseDisposition/allow), the task continues as a data task.
- If you pass [`URLSession.ResponseDisposition.cancel`](/documentation/Foundation/URLSession/ResponseDisposition/cancel), the task is canceled.
- If you pass [`URLSession.ResponseDisposition.becomeDownload`](/documentation/Foundation/URLSession/ResponseDisposition/becomeDownload), your delegate’s [`urlSession(_:dataTask:didBecome:)`](/documentation/Foundation/URLSessionDataDelegate/urlSession(_:dataTask:didBecome:)-60op5) method is called to provide the new download task that supersedes the current task.

## Discussion

Implementing this method is optional unless you need to cancel the transfer or convert it to a download task when the response headers are first received. If you don’t provide this delegate method, the session always allows the task to continue.

You also implement this method if you need to support the fairly obscure `multipart/x-mixed-replace` content type. With that content type, the server sends a series of parts, each of which is intended to replace the previous part. The session calls this method at the beginning of each part, followed by one or more calls to [`urlSession(_:dataTask:didReceive:)`](/documentation/Foundation/URLSessionDataDelegate/urlSession(_:dataTask:didReceive:)) with the contents of that part.

Each time the [`urlSession(_:dataTask:didReceive:completionHandler:)`](/documentation/Foundation/URLSessionDataDelegate/urlSession(_:dataTask:didReceive:completionHandler:)) method is called for a part, collect the data received for the previous part (if any) and process the data as needed for your application. This processing can include storing the data to the filesystem, parsing it into custom types, or displaying it to the user. Next, begin receiving the next part by calling the completion handler with the [`URLSession.ResponseDisposition.allow`](/documentation/Foundation/URLSession/ResponseDisposition/allow) constant. Finally, if you have also implemented [`urlSession(_:task:didCompleteWithError:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:didCompleteWithError:)), the session will call it after sending all the data for the last part.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)