UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.


Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.