I’m having a very odd problem in which my URLSession response works the first time, but almost always fails on subsequent calls. It’s taken me forever to debug because I needed to examine the response headers to determine anything at all. I used Proxyman based on a recommendation from Donny Wals -- https://www.donnywals.com/debugging-network-traffic-with-proxyman/ -- and — as far as I can tell — the only differences between the calls is that the first call returns:
HTTP/1.1 200 OK
Date: Tue, 06 Jun 2023 14:06:08 GMT
Server: Apache
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control
Vary: Accept-Encoding,User-Agent
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Encoding: gzip
Set-Cookie: PHPSESSID=d10701af2da595b698c57f183e76a709; path=/
Upgrade: h2
Connection: Upgrade, Keep-Alive
Keep-Alive: timeout=5, max=100
Transfer-Encoding: chunked
While subsequent calls return different values for Keep-Alive and Connection (and they’re returned in a different order):
Second call same as first except:
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Third Call same as first and second except:
Upgrade: h2
Connection: Upgrade, Keep-Alive
Keep-Alive: timeout=5, max=100
The subsequent calls also do not return the Set-Cookie value, and the Upgrade value appears sometimes but not others.
All three return 200 OK, but my code is failing on this step:
guard let (data, response) = try? await session.data(for: request),
let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200
else {
logger.debug("Failed to receive valid response and/or data.")
throw MyError.missingData
}
This code is taken directly from the Earthquakes project at Apple (although I modified it slightly to send a POST to my API, which seems to work fine).
What's even more frustrating is that the second and third call immediately return the correct JSON data (all formatted fine, etc.) just like the first call, so I cannot understand why they fail.
I am completely stumped and hoping someone might have some idea of what I can do here, or even how I can debug better.
Thanks immensely for any help you can provide.