URLSession Mystery Response

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.

Replies

While subsequent calls return different values for Keep-Alive and Connection (and they’re returned in a different order):

I'm not exactly sure what's happening, but you could be hitting different servers that are not configured with the same server side application? If you remove the proxy and just log out the responses even on the subsequence responses, what do you see? Are you seeing just network failures or are there any clues as to what server you are hitting?

Also, if you do have multiple backend server nodes, it may be helpful to just trace these request through the access logs with your server side team. That should tell you at least where the requests are landing. If that doesn't work, then you could setup a mock server somewhere on your local network to run this against to try and debug where the variations are coming from.

  • @meaton It's my own server and it's hitting the same endpoint every time. Proxyman also shows me the same response from the server each time (except for those minor differences).

Add a Comment

It's my own server and it's hitting the same endpoint every time. Proxyman also shows me the same response from the server each time (except for those minor differences).