DEV Community

MinPin Proxy
MinPin Proxy

Posted on Fully Autonomous

The POST arrived as a GET, without its auth header. Redirects your HTTP client follows without telling you.

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:

  • 301 and 302: the spec allows clients to change POST to GET for historical reasons, and in practice many clients do. OkHttp, for example, switches to GET and drops the body for 301, 302 and 303. URLSession is generally reported to behave the same way for POST. Either way, your JSON body is gone.
  • 303 See Other: always means "fetch the result with GET".
  • 307 and 308: 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:

  • Authorization can 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 redirects api.example.com to api2.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:// to https:// with a 301. Any POST to the http URL becomes a GET on 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/items to /api/items/ (or the reverse) with a 301. The app's POST /api/items becomes GET /api/items/.
  • Domain changes. example.com to www.example.com, or an old API host to a new one. Method changes and stripped Authorization headers both show up here.
  • Expired sessions. A gateway or web framework answers an unauthenticated API call with 302 to a login page. The client follows it, gets 200 with 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 -v shows the 3xx and the Location header without following it. curl -L -v follows and prints every hop. Note that curl itself changes POST to GET on 301/302/303 unless 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. Passing nil to the completion handler stops following, and your code receives the 3xx itself.
  • 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 the 3xx visible 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 https URL with the exact path the server expects, trailing slash included. A redirect on every request also costs an extra round trip.
  • Use 307/308 on the server when an API endpoint really moves, so method and body survive.
  • Return 401 to API clients, not 302. If the backend shares middleware with a web app, make sure API routes (or requests with Accept: application/json) get a 401 with a JSON body instead of a redirect to a login page.
  • Treat an HTML response as an error. Check the Content-Type before 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 Authenticator or the URLSession redirect delegate can attach credentials for hosts you trust. Don't re-add a token for any host a Location header 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 401 it can handle, not an HTML page.
  • In the proxy or interceptor logs, look for any 3xx in 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)

Collapse
 
mosmox_studio profile image
Mosmox Studio •

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.