{"action":"created","issue":{"url":"https://api.github.com/repos/Alamofire/Alamofire/issues/853","labels_url":"https://api.github.com/repos/Alamofire/Alamofire/issues/853/labels{/name}","comments_url":"https://api.github.com/repos/Alamofire/Alamofire/issues/853/comments","events_url":"https://api.github.com/repos/Alamofire/Alamofire/issues/853/events","html_url":"https://github.com/Alamofire/Alamofire/issues/853","id":111001616,"number":853,"title":"Serializer reports success when HTTP response is 500","user":{"login":"josephknelf","id":11778416,"avatar_url":"https://avatars.githubusercontent.com/u/11778416?v=3","gravatar_id":"","url":"https://api.github.com/users/josephknelf","html_url":"https://github.com/josephknelf","followers_url":"https://api.github.com/users/josephknelf/followers","following_url":"https://api.github.com/users/josephknelf/following{/other_user}","gists_url":"https://api.github.com/users/josephknelf/gists{/gist_id}","starred_url":"https://api.github.com/users/josephknelf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josephknelf/subscriptions","organizations_url":"https://api.github.com/users/josephknelf/orgs","repos_url":"https://api.github.com/users/josephknelf/repos","events_url":"https://api.github.com/users/josephknelf/events{/privacy}","received_events_url":"https://api.github.com/users/josephknelf/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-12T15:38:13Z","updated_at":"2015-10-13T03:11:42Z","closed_at":"2015-10-13T03:11:42Z","body":"I am uploading a file to a server which responds, occasionally, with an error 500. The problem is that the result is marked as a success and this confuses my client code something fierce. Here's an example of the problem:\r\n\r\n         let endpointUrl = NSURL(\"http://aserver.com/somepath/uploadImageEndpoint\")\r\n        \r\n        let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData\r\n        \r\n        let request = NSMutableURLRequest(URL: endpointUrl!, cachePolicy: cachePolicy, timeoutInterval: 60.0)\r\n        request.HTTPMethod = \"POST\"\r\n        \r\n        // set credentials\r\n        let userCredentials = UserCredentials()\r\n        let base64EncodedCredentials = userCredentials.base64EncodedCredentials()\r\n        request.setValue(\"Basic \\(base64EncodedCredentials)\", forHTTPHeaderField: \"Authorization\")\r\n        \r\n        // set boundary\r\n        let boundary = \"NET-POST-boundary-\\(arc4random())-\\(arc4random())\"\r\n        request.setValue(\"multipart/form-data;boundary=\"+boundary,\r\n            forHTTPHeaderField: \"Content-Type\")\r\n        \r\n        // create request parameters\r\n        let parameters = NSMutableData()\r\n        for s in [\"\\r\\n--\\(boundary)\\r\\n\",\r\n            \"Content-Disposition: form-data; filename=\\\"userimage.png\\\"\\r\\n\",\r\n            \"Content-Type: image/png\\r\\n\\r\\n\"] {\r\n                parameters.appendData(s.dataUsingEncoding(NSUTF8StringEncoding)!)\r\n        }\r\n        parameters.appendData(UIImagePNGRepresentation(image)!)\r\n        parameters.appendData(\"\\r\\n--\\(boundary)--\\r\\n\"\r\n            .dataUsingEncoding(NSUTF8StringEncoding)!)\r\n       \r\n        upload(request, data: parameters).responseData() { (_, response, result) in\r\n            if(response?.statusCode == 500) {\r\n                NSLog(\"@@@@@@@@@@@@@@@ Received 500\")\r\n            }\r\n            if(result.isSuccess) {\r\n                NSLog(\"@@@@@@@@@@@@@@@ result is success\")\r\n            }\r\n            if(result.isFailure) {\r\n                NSLog(\"@@@@@@@@@@@@@@@ result is failure\")\r\n            }\r\n            switch result {\r\n                case .Success(_):\r\n                    successHandler()\r\n                case .Failure(_, let error):\r\n                    let nsError = error as NSError\r\n                    errorHandler(nsError)\r\n            }\r\n        }\r\n\r\nThis will output:\r\n\r\n@@@@@@@@@@@@@@@ Received 500\r\n@@@@@@@@@@@@@@@ result is success\r\n\r\nI don't believe this an intuitive result. The result should be a failure if the server returns a 500.\r\n\r\nThis issue was encountered in XCode 7.0.1 running an iPhone 6 simulator which is simulating iOS 9.0.1"},"comment":{"url":"https://api.github.com/repos/Alamofire/Alamofire/issues/comments/147583034","html_url":"https://github.com/Alamofire/Alamofire/issues/853#issuecomment-147583034","issue_url":"https://api.github.com/repos/Alamofire/Alamofire/issues/853","id":147583034,"user":{"login":"jshier","id":51020,"avatar_url":"https://avatars.githubusercontent.com/u/51020?v=3","gravatar_id":"","url":"https://api.github.com/users/jshier","html_url":"https://github.com/jshier","followers_url":"https://api.github.com/users/jshier/followers","following_url":"https://api.github.com/users/jshier/following{/other_user}","gists_url":"https://api.github.com/users/jshier/gists{/gist_id}","starred_url":"https://api.github.com/users/jshier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jshier/subscriptions","organizations_url":"https://api.github.com/users/jshier/orgs","repos_url":"https://api.github.com/users/jshier/repos","events_url":"https://api.github.com/users/jshier/events{/privacy}","received_events_url":"https://api.github.com/users/jshier/received_events","type":"User","site_admin":false},"created_at":"2015-10-13T03:11:42Z","updated_at":"2015-10-13T03:11:42Z","body":"By default Alamofire registers any response that makes it through the response serializer (in your example, the `dataResponseSerializer` used by `.responseData`) as a success. If you wish to validate the response code of your request, just add a `.validate()` to your request chain. This will ensure that your response code is between 200 and 299, inclusive. You can also customize validation using the [methods described in the documentation] (https://github.com/Alamofire/Alamofire#validation)."}}