Sometimes (30%) Status Code 400: Bad Access when downloading using NSURLSession.

Ok, so sometimes (like in 30% of the times) I get a status code 400 (Bad Access) when downloading a file. When I afterwards try to download it again it works.

My Code:


private func downloadData(customProperty:BLCustomProperty) {
        
        let appId = BackendlessManager.sharedInstance.backendless.appID
        let version = BackendlessManager.sharedInstance.backendless.versionNum
        let file = customProperty.dataProperty!
        
        let url = ("https://api.backendless.com/\(appId)/\(version)/files/\(file)")
        print ("download: \(url)")
        
        let request = NSURLRequest(URL: NSURL(string: url)!)
        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session = NSURLSession(configuration: config)
        
        let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
            
            // notice that I can omit the types of data, response and error
            
            // your code
            if let err = error {
                print ("DOWNLOAD FAILED! Handle error -- NOT IMPLEMENTED YET!!")
                print ("Error: \(err.description)")
            } else {
                print("Download Finished!")
                print("response: \(response)")
                
                if let data = data {
                    if let cdCustomProperty = self.cdHelper.getCustomProperty(backendID: customProperty.objectId) {
                        cdCustomProperty.dataProperty!.data = data
                        
                        // Check if there are more data properties that need downloading
                        dispatch_async(dispatch_get_main_queue()) {
                            self.dataToDownload(customProperty.projectObject!)
                        }
                    } else {
                        print("1 - THIS SHOULD NOT HAPPEN?! -- NOT IMPLEMENTED YET")
                    }
                } else {
                    print("2 - THIS SHOULD NOT HAPPEN?! -- NOT IMPLEMENTED YET")
                }
            }
            
        });
        
        // Run the task
        task.resume()

Here is a logged 400 Bad Access situation:


Download Finished!
response: Optional(<NSHTTPURLResponse: 0x7f87336d1b60> { URL: https://api.backendless.com/703E6F9E-255E-1662-FF44-BC268FD5C800/v1/files/projectObject_objectData/2016/2/19/37E64D12-2EE0-4B32-AC9D-A007CAB94E4F.data } { status code: 400, headers {
    "Access-Control-Allow-Headers" = "origin, application-id, application-type, content-type, secret-key, request, user-token";
    "Access-Control-Allow-Methods" = "POST, GET, OPTIONS, PUT, DELETE, PATCH";
    "Access-Control-Allow-Origin" = "*";
    Connection = "keep-alive";
    "Content-Length" = 62;
    "Content-Type" = "application/json";
    Date = "Fri, 19 Feb 2016 14:42:02 GMT";
    Server = "nginx/1.4.2";
} })

Barry,

We have checked and could not reproduce the problem, the file can be downloaded every time we tried.

Mark

I can too Mark, using wget or a browser. However using the posted code it gives me the 400. I created a workaround for now (check for status 200 or else retry). I’ll report back later this week how it goes (if the problem is still there).