The bug report: the endpoint works in the API docs' "try it" page, works in curl, and from the app the server logs show a GET with no body where the app sent a POST. Or the request arrives without its Authorization header and gets a 401. Or the app crashes decoding JSON because the "response" starts with <!DOCTYPE html>.
The app's code never mentions a redirect. That's the problem: URLSession and OkHttp follow redirects by default, so the request your code built and the request that reached the final server can be different. Here's what changes on the way and how to see it.
What a redirect does to your request
The status code decides what happens to the method and the body:
-
301and302: the spec allows clients to changePOSTtoGETfor historical reasons, and in practice many clients do. OkHttp, for example, switches toGETand drops the body for301,302and303. URLSession is generally reported to behave the same way forPOST. Either way, your JSON body is gone. -
303 See Other: always means "fetch the result withGET". -
307and308: the method and body must be kept. If a server needs a redirect for an API endpoint, these are the codes to use.
Headers change too:
-
Authorizationcan be removed when the redirect goes to a different host. OkHttp does this on purpose, so a token isn't sent to a server it wasn't meant for. If your API redirectsapi.example.comtoapi2.example.com, the second request arrives unauthenticated. - Custom headers are generally kept by OkHttp and URLSession when following a redirect, but behavior differs between libraries and versions, so check rather than assume.
Where the redirects come from
Most of them aren't intentional API design:
-
HTTP to HTTPS. A load balancer redirects
http://tohttps://with a301. AnyPOSTto thehttpURL becomes aGETon the way. On iOS, App Transport Security usually blocks the plain-HTTP request first. On Android, cleartext traffic is blocked by default for apps targeting Android 9 (API 28) or higher, unless the network security config allows it. -
Trailing slashes. Some frameworks redirect
/api/itemsto/api/items/(or the reverse) with a301. The app'sPOST /api/itemsbecomesGET /api/items/. -
Domain changes.
example.comtowww.example.com, or an old API host to a new one. Method changes and strippedAuthorizationheaders both show up here. -
Expired sessions. A gateway or web framework answers an unauthenticated API call with
302to a login page. The client follows it, gets200with HTML, and the JSON decoder fails with an error about an unexpected<. The real problem was an expired token, but the error points at parsing.
How to see the redirect chain
-
curl:
curl -vshows the3xxand theLocationheader without following it.curl -L -vfollows and prints every hop. Note that curl itself changesPOSTtoGETon301/302/303unless told otherwise (--post301,--post302), so it's a fair stand-in for a mobile client. -
iOS: implement
urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)on the session delegate. Log the response status, the old URL, and the new request's method and headers. Passingnilto the completion handler stops following, and your code receives the3xxitself. -
Android (OkHttp): a network interceptor (
addNetworkInterceptor) sees every hop, including each redirect response, while an application interceptor only sees the final result. For debugging,followRedirects(false)makes the3xxvisible to your code. - A debugging proxy or network inspector shows each hop as a separate request, which makes the method change and the missing header obvious side by side.
Fixing it
-
Call the final URL. In the app's config, use the
httpsURL with the exact path the server expects, trailing slash included. A redirect on every request also costs an extra round trip. -
Use
307/308on the server when an API endpoint really moves, so method and body survive. -
Return
401to API clients, not302. If the backend shares middleware with a web app, make sure API routes (or requests withAccept: application/json) get a401with a JSON body instead of a redirect to a login page. -
Treat an HTML response as an error. Check the
Content-Typebefore decoding, and report "unexpected HTML response" with the status and final URL instead of a decoder error. -
Re-add auth deliberately, if at all. If a cross-host redirect is expected, an OkHttp
Authenticatoror the URLSession redirect delegate can attach credentials for hosts you trust. Don't re-add a token for any host aLocationheader names.
Testing it
- Point a debug build at the
http://URL of each API host and confirm what happens: blocked, or redirected with the method intact. - Call a path with and without a trailing slash and check the server's response code.
- Expire a token on purpose and check that the app gets a
401it can handle, not an HTML page. - In the proxy or interceptor logs, look for any
3xxin normal traffic. Each one is either a wasted round trip or a request that changed on the way.
This article was created with the help of AI.
Top comments (1)
The 302-to-login one is very common with Laravel or Rails backends where API routes share the web auth middleware: Laravel's default unauthenticated handler redirects unless the request expects JSON. Having the app send Accept: application/json on every call removes a whole class of these HTML-instead-of-JSON errors.