Upload a zip file in a background URLSession.

I'm trying to upload an existing zip file to a server, but the file itself doesn't seem to be present in the request when I check what the server actually receives. Here's my current implementation:

func uploadZip(filePath: URL, authorization: String, callback: @escaping ((Bool) -> Void)) {
    BGUploadManager.callback = callback
    let url = URL(string: "\(APINetwork.BASE_URL)")!
        
    var request = URLRequest(url: url)
    request.headers = ["Authorization": "Bearer " + authorization]
    request.method = HTTPMethod(rawValue: "POST")
    request.setValue("application/zip", forHTTPHeaderField: "Content-Type")
        
    let config = URLSessionConfiguration.background(withIdentifier: "identifier")
    config.isDiscretionary = false

    let session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
        
    let task = session.uploadTask(with: request, fromFile: filePath)
    task.resume()
}

There are implemented handlers for completion and progress and they work properly. The problem is really just the actual file not being sent (or if it is I don't know how to get it). Here's parts of what the server receives:

headers:
   { host: 'localhost:1337',
     'content-type': 'application/zip',
     'accept-encoding': 'gzip, deflate',
     connection: 'keep-alive',
     accept: '*/*',
     'user-agent': 'App/37 CFNetwork/1237 Darwin/20.4.0',
     authorization:
      'Bearer sometokenhere',
     'content-length': '89634' }
body: {}

Checking for request.files also shows that it's empty. The Content-Length value is correct. The filePath is the full path and does actually point to the zip file.

Answered by DTS Engineer in 683534022

Honestly, it sounds like you have server-side problems. Some advice:

  • Temporarily switch to using a standard NSURLSession rather than a background session. That makes debugging this sort of thing much easier.

  • Use the techniques in Debugging HTTP Server-Side Errors to inspect the request and confirm that it’s in the format you expect. That’ll tell you whether you should debug this on the client side or the server side.

ps If you have access to the current beta OS releases you can use the shiny new HTTP support in Instruments to debug this. For more info, see WWDC 2021 Session 10212 Analyze HTTP traffic in Instruments.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Honestly, it sounds like you have server-side problems. Some advice:

  • Temporarily switch to using a standard NSURLSession rather than a background session. That makes debugging this sort of thing much easier.

  • Use the techniques in Debugging HTTP Server-Side Errors to inspect the request and confirm that it’s in the format you expect. That’ll tell you whether you should debug this on the client side or the server side.

ps If you have access to the current beta OS releases you can use the shiny new HTTP support in Instruments to debug this. For more info, see WWDC 2021 Session 10212 Analyze HTTP traffic in Instruments.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Upload a zip file in a background URLSession.
 
 
Q