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 = ("\(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()) {
                    } else {
                        print("1 - THIS SHOULD NOT HAPPEN?! -- NOT IMPLEMENTED YET")
                } else {
                    print("2 - THIS SHOULD NOT HAPPEN?! -- NOT IMPLEMENTED YET")
        // Run the task

Here is a logged 400 Bad Access situation:

Download Finished!
response: Optional(<NSHTTPURLResponse: 0x7f87336d1b60> { URL: } { 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";
} })


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


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).