Call made like this
let jsonData = try JSONSerialization.data(withJSONObject: requestBody)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let (data, response) = try await URLSession.shared.data(for: request)
When I call it from the simulator it works perfect 100% of the time.
When I run it on the phone while on WiFi it works perfect 100% of the time.
BUT when I run it on the phone on cell network. It fails randomly.
But if I try to call that end point using another API test app on the phone. It works.
The error I get back when it fails as follows:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=57, NSUnderlyingError=0x28198df50 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x2833f6e20 [0x1e1c79d10]>{length = 28, capacity = 28, bytes = 0x1c1e01bb000000002606470000200000 ... 681a00aa00000000}, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask .<2>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask .<2>" ), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=https://blahblah/v1/run, NSErrorFailingURLKey=https://blahblah/v1/run, _kCFStreamErrorDomainKey=1}
How large is your POST body? Cellular networking can be quite flaky at times, and losing a connection when uploading large files is not uncommon.
If you're uploading big chunks of data, a good strategy is to change the server side to allow you to upload the data in chunks, retrying each chunk until it succeeds, then combine the chunks on the server side after you finish uploading everything.
Otherwise, the next question you should ask is whether you can make the request idempotent. If it is possible to make the request in such a way that sending it twice won't cause any harm, then you should be using a GET request so that the NSURLSession machinery can retry the request after certain types of network failures, network changes, etc. POST requests are never retried.
The third thing you should ask is whether you are overriding any of the session's behavior using callbacks. If so, are you certain that you are calling the provided callback every time? Some common mistakes I've seen include:
and so on. Any of these things can result in very strange behavior.
Beyond that, look for any differences in the way you're setting up the session and connection between the app that works and the app that doesn't — differences in the session configuration, differences in shared cache configuration, differences in task creation, differences in threading model, etc.
Good luck.