Hello,
I have a problem with uploading data via URLSession when the server responds with an error.
Specifically, I upload data with URLSession.upload(for:from:delegate)
. The server determines that the data is too large and responds with http status 413 (Payload Too Large). However, that response is not returned by URLSession.upload. Instead, it waits until a timeout occurs and throws that as an error.
The weird part is: in the simulator it works perfectly fine and the server response is returned immediately as one would expect. But on an actual device (iPhone, iOS 16.3), it always runs into the timeout. I tested this with the same data.
In Xcode's console, I see an interesting message: "Connection 1: received failure notification". I do not see that message in the console when running the app in the simulator. (I tried to include more log messages but the forum software wouldn't let me).
I also tried to activate network debugging via CFNETWORK_DIAGNOSTICS, but could not make much sense of it. But I do see that the server response is received almost immediately. So the response is received by the network layer, just not returned by URLSession.
By the way, calling upload(for:fromFile:delegate) behaves the same way. As does using a completion handler instead of async/await.
I'm at a loss because I don't think our code is wrong. And I'm completely puzzled by the fact that it works as expected in the simulator.