URLSession 302 redirect

Hi,


I'm wondering would the network request be successful if a 302 redirect is placed on the server and the app doesn't explicitly implement 'willPerformHTTPRedirection' delegate?

We are working on domain migrations and are concerned about backward compatibility of apps to make successful requests(200) when the url redirects are effective.

Appreciate the help.

Replies

URLSession
will automatically follow redirects. You can use the ‘will perform HTTP redirection’ delegate method to override that default behaviour [1], but the default is to follow redirects.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] In standard sessions; this delegate isn’t called in background sessions.

If a server responds with a 302 redirect and the app does not explicitly implement the willPerformHTTPRedirection delegate method, the network request will follow the redirect automatically. In this case, the redirect will be handled by the underlying networking framework used by the app, such as URLSession or Alamofire.

The default behavior of these networking frameworks is to handle redirects automatically, unless explicitly configured otherwise. The frameworks will typically follow the redirect and make a new request to the URL specified in the redirect response.

Therefore, if the server responds with a 302 redirect and the new URL specified in the redirect response is valid and accessible, the subsequent network request should be successful (assuming there are no other issues).

However, if the new URL is not valid or accessible, or if there are any network connectivity issues, the subsequent request may fail. In such cases, the app would typically receive an error or failure response, rather than a successful 200 response.

To ensure successful requests during domain migrations, it's important to test the redirects thoroughly on different versions of the app and network conditions. You should also consider implementing appropriate error handling and fallback mechanisms in the app to handle cases where the redirect fails or encounters errors.

Additionally, if you have control over the app's codebase, implementing the willPerformHTTPRedirection delegate method allows you to have more control over the redirect behavior and handle it in a customized manner if needed.

Overall, if the server is set up to respond with a 302 redirect and the app is using a standard networking framework, the redirect should be followed automatically by the app, and a successful request (200 response) can be expected if the new URL is valid and accessible.

I hope this clarifies the behavior of redirects in the context of app networking. Let me know if you have any further questions.