I would like to receive an HTTP response status code (e.g. 400, 401, 403, 503, etc.) for request failures (and ideally for success). In this code, I authenticate the user using HTTP Basic and want to be able to tell the user that the authentication failed when the user mistakenly uses his password.
Alamofire.request(.GET, "https://host.com/a/path").authenticate(user: "user", password: "typo") .responseString { (req, res, data, error) in if error != nil { println("STRING Error:: error:\(error)") println(" req:\(req)") println(" res:\(res)") println(" data:\(data)") return } println("SUCCESS for String") } .responseJSON { (req, res, data, error) in if error != nil { println("JSON Error:: error:\(error)") println(" req:\(req)") println(" res:\(res)") println(" data:\(data)") return } println("SUCCESS for JSON") }
Unfortunately, the error received does not indicate that an HTTP 409 status code was actually received:
STRING Error:: error:Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x7f9beb8efce0 {NSErrorFailingURLKey=https://host.com/a/path, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://host.com/a/path}) req:<NSMutableURLRequest: 0x7f9beb89d5e0> { URL: https://host.com/a/path } res:nil data:Optional("") JSON Error:: error:Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x7f9beb8efce0 {NSErrorFailingURLKey=https://host.com/a/path, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://host.com/a/path}) req:<NSMutableURLRequest: 0x7f9beb89d5e0> { URL: https://host.com/a/path } res:nil data:nil
In addition, it would be nice to get the HTTP body when an error occurs, because my server side will post a text description of the error there.
Questions
Is it possible to get a status code with a response not 2xx?
Is it possible to get a specific status code with a 2xx response?
Is it possible to get an HTTP body with a non-2xx response?
Thank!
swift alamofire
GregT Mar 18 '15 at 19:39 2015-03-18 19:39
source share