<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.2.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSURLConnectionDataDelegate/connection(_:willSend:redirectResponse:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLConnectionDataDelegate(im)connection:willSendRequest:redirectResponse:"
  },
  "title" : "connection(_:willSend:redirectResponse:)"
}
-->

# connection(_:willSend:redirectResponse:)

Sent when the connection determines that it must change URLs in order to continue loading a request.

```
optional func connection(_ connection: NSURLConnection, willSend request: URLRequest, redirectResponse response: URLResponse?) -> URLRequest?
```

## Parameters

`connection`

The connection sending the message.

`request`

The proposed redirected request. The delegate should inspect the redirected request to verify that it meets its needs, and create a copy with new attributes to return to the connection if necessary.

`response`

The URL response that caused the redirect. May be `nil` in cases where this method is called because of URL canonicalization.

## Return Value

The actual URL request to use in light of the redirection response. The delegate may return `request` unmodified to allow the redirect, return a new request, or return `nil` to reject the redirect and continue processing the connection.

## Discussion

If `redirectResponse` is `nil`, the URL was canonicalized (rewritten into its standard form) by the [`URLProtocol`](/documentation/Foundation/URLProtocol) object handling the request. Update your user interface to show the standardized form of the URL, then return the original request unmodified.

Otherwise, to cancel the redirect, call the `connection` object’s `cancel` method, then return the provided request object.

To receive the body of the redirect response itself, return `nil` to cancel the redirect. The connection continues to process, eventually sending your delegate a `connectionDidFinishLoading` or `connection:didFailLoadingWithError:` message, as appropriate.

To redirect the request to a different URL, create a new request object and return it.

> Important:
> Your delegate method is called on the thread where the connection is scheduled. If you call the `cancel` method, do so on that same thread.

The delegate should be prepared to receive this message multiple times.

---

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)