<!--
{
  "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/URLSessionTaskDelegate/urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLSessionTaskDelegate(im)URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:"
  },
  "title" : "urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)"
}
-->

# urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)

Tells the delegate that the remote server requested an HTTP redirect.

```
optional func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping @Sendable (URLRequest?) -> Void)
```

## Parameters

`session`

The session containing the task whose request resulted in a redirect.

`task`

The task whose request resulted in a redirect.

`response`

An object containing the server’s response to the original request.

`request`

A URL request object filled out with the new location.

`completionHandler`

A block that your handler should call with either the value of the `request` parameter, a modified URL request object, or `NULL` to refuse the redirect and return the body of the redirect response.

## Discussion

This method is called *only* for tasks in default and ephemeral sessions. Tasks in background sessions automatically follow redirects.

---

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)