{"id":7801,"date":"2015-10-27T09:21:13","date_gmt":"2015-10-27T09:21:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/27\/swift-alamofire-how-to-get-the-http-response-status-code-open-source-projects-alamofire-alamofire\/"},"modified":"2015-10-27T09:21:13","modified_gmt":"2015-10-27T09:21:13","slug":"swift-alamofire-how-to-get-the-http-response-status-code-open-source-projects-alamofire-alamofire","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/27\/swift-alamofire-how-to-get-the-http-response-status-code-open-source-projects-alamofire-alamofire\/","title":{"rendered":"Swift Alamofire: How to get the HTTP response status code-open source projects Alamofire\/Alamofire"},"content":{"rendered":"<p>I would like to retrieve the HTTP response status code (e.g. 400, 401, 403, 503, etc) for request failures (and ideally for successes too). In this code, I am performing user authentication with HTTP Basic and want to be able to message the user that authentication failed when the user mistypes their password.<\/p>\n<pre><code>Alamofire.request(.GET, \"https:\/\/host.com\/a\/path\").authenticate(user: \"user\", password: \"typo\")\n    .responseString { (req, res, data, error) in\n        if error != nil {\n            println(\"STRING Error:: error:\\(error)\")\n            println(\"  req:\\(req)\")\n            println(\"  res:\\(res)\")\n            println(\"  data:\\(data)\")\n            return\n        }\n        println(\"SUCCESS for String\")\n}\n    .responseJSON { (req, res, data, error) in\n        if error != nil {\n            println(\"JSON Error:: error:\\(error)\")\n            println(\"  req:\\(req)\")\n            println(\"  res:\\(res)\")\n            println(\"  data:\\(data)\")\n            return\n        }\n        println(\"SUCCESS for JSON\")\n}\n<\/code><\/pre>\n<p>Unfortunately, the error produced does not seem to indicate that an HTTP status code 409 was actually received:<\/p>\n<pre><code>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})\n  req: { URL: https:\/\/host.com\/a\/path }\n  res:nil\n  data:Optional(\"\")\nJSON 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})\n  req: { URL: https:\/\/host.com\/a\/path }\n  res:nil\n  data:nil\n<\/code><\/pre>\n<p>Additionally, it would be nice to retrieve the HTTP body when an error occurs because my server-side will put a textual description of the error there.<\/p>\n<p><strong>Questions<\/strong> Is it possible to retrieve the status code upon a non-2xx response? Is it possible to retrieve the specific status code upon a 2xx response?<\/p>\n<p>Is it possible to retrieve the HTTP body upon a non-2xx response?<\/p>\n<p>Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would like to retrieve the HTTP response status code (e.g. 400, 401, 403, 503, etc) for request failures (and ideally for successes too). In this code, I am performing user authentication with HTTP Basic and want to be able to message the user that authentication failed when the user mistypes their password. Alamofire.request(.GET, &#8220;https:\/\/host.com\/a\/path&#8221;).authenticate(user: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7801","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7801"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7801\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}