Back to homepage

What Happens When Your Mobile API Returns a Redirect?

A proxy adds a trailing slash and returns 302. Your HTTP client can turn the JSON POST into a GET and hand you a 200 with HTML. That looks like a decoding bug. A 307 or 308 can resend the POST body. A redirect to another origin can expose sensitive payloads or headers, such as a custom API key header, if the client forwards them. RFC 9110

Use the final HTTPS URL and disable redirects for API calls. Check the original HTTP status before decoding the body.

iOS · URLSession

import Foundation

final class StopRedirects: NSObject, URLSessionTaskDelegate {
    func urlSession(
        _ session: URLSession,
        task: URLSessionTask,
        willPerformHTTPRedirection response: HTTPURLResponse,
        newRequest request: URLRequest
    ) async -> URLRequest? { nil }
}

let (data, response) = try await URLSession.shared.data(
    for: request, delegate: StopRedirects()
)

Android · OkHttp

val apiClient = sharedOkHttpClient.newBuilder()
    .followRedirects(false)
    .followSslRedirects(false)
    .build()