403 Error with URLSession.downloadTask (SwiftUI)

I wrote a program that can download an updated data file publicly available from the US Government's FCC website. The file is updated weekly. This week, I went to download the file with my program and it failed to download. I looked at the HTTPURLResponse parameter to the completion callback and it shows a statusCode of 403 (forbidden). The program is the same program as last week when it worked. I copied the exact URL that the program was trying to download to both Safari and the command line curl and both were able to download the file without issue on the same computer. I changed the program to try and download a different file from a different server and it downloaded fine.

I tried running the same program on a different Mac and it failed also.

I tried several times for over an hour with the same result.

Browsing the web for a solution I see mention of clearing caches. How does one clear the caches for a SwiftUI program?

Thanks, Mark

Answered by MarkErbaugh in 696011022

I think I figured it out. URLSession uses a user agent of the application bundle name / build. Somehow the build setting on my program was cleared so the user agent was / (Unknown version number). That was causing the FCC's server to reject my request. I change the build number in Xcode and I can download my file.

More information:

I copied the download code from this program and created a new program. It is able to download the file (getting an HTTP status code of 200).

So I now think there is something cached in the original program that is causing it to be blocked. If this were a browser, I would clear the caches/cookies/etc. How can I do that with URLSession?

I deleted the CACHES folder under the Library/Containers. I even deleted the entire sandbox under Library/Containers (the program rebuilt it). Still getting the 403 error.

Accepted Answer

I think I figured it out. URLSession uses a user agent of the application bundle name / build. Somehow the build setting on my program was cleared so the user agent was / (Unknown version number). That was causing the FCC's server to reject my request. I change the build number in Xcode and I can download my file.

403 Error with URLSession.downloadTask (SwiftUI)
 
 
Q