WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

URLSession Mystery Response

Forums > SwiftUI

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 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:

Keep-Alive: timeout=5, max=99
Connection: Keep-Alive

Third Call:

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 you might have some idea of what I can do here.

Thanks immensely for any help you can provide.


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.