Swift Alamofire: How to get the HTTP response status code
Asked Answered
F

12

127

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, "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 produced does not seem to indicate that an HTTP status code 409 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

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.

Questions
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?
Is it possible to retrieve the HTTP body upon a non-2xx response?

Thanks!

Fungi answered 18/3, 2015 at 19:39 Comment(1)
If you are unauthenticated you receive a -999 by design. Not sure why this is or how it can be solved... Did you solve this?Heterocyclic
C
215

For Swift 3.x / Swift 4.0 / Swift 5.0 users with Alamofire >= 4.0 / Alamofire >= 5.0


response.response?.statusCode

More verbose example:

Alamofire.request(urlString)
        .responseString { response in
            print("Success: \(response.result.isSuccess)")
            print("Response String: \(response.result.value)")

            var statusCode = response.response?.statusCode
            if let error = response.result.error as? AFError {  
                statusCode = error._code // statusCode private                 
                switch error {
                case .invalidURL(let url):
                    print("Invalid URL: \(url) - \(error.localizedDescription)")
                case .parameterEncodingFailed(let reason):
                    print("Parameter encoding failed: \(error.localizedDescription)")
                    print("Failure Reason: \(reason)")
                case .multipartEncodingFailed(let reason):
                    print("Multipart encoding failed: \(error.localizedDescription)")
                    print("Failure Reason: \(reason)")
                case .responseValidationFailed(let reason):
                    print("Response validation failed: \(error.localizedDescription)")
                    print("Failure Reason: \(reason)")

                    switch reason {
                    case .dataFileNil, .dataFileReadFailed:
                        print("Downloaded file could not be read")
                    case .missingContentType(let acceptableContentTypes):
                        print("Content Type Missing: \(acceptableContentTypes)")
                    case .unacceptableContentType(let acceptableContentTypes, let responseContentType):
                        print("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
                    case .unacceptableStatusCode(let code):
                        print("Response status code was unacceptable: \(code)")
                        statusCode = code
                    }
                case .responseSerializationFailed(let reason):
                    print("Response serialization failed: \(error.localizedDescription)")
                    print("Failure Reason: \(reason)")
                    // statusCode = 3840 ???? maybe..
                default:break
                }

                print("Underlying error: \(error.underlyingError)")
            } else if let error = response.result.error as? URLError {
                print("URLError occurred: \(error)")
            } else {
                print("Unknown error: \(response.result.error)")
            }

            print(statusCode) // the status code
    } 

(Alamofire 4 contains a completely new error system, look here for details)

For Swift 2.x users with Alamofire >= 3.0

Alamofire.request(.GET, urlString)
      .responseString { response in
             print("Success: \(response.result.isSuccess)")
             print("Response String: \(response.result.value)")
             if let alamoError = response.result.error {
               let alamoCode = alamoError.code
               let statusCode = (response.response?.statusCode)!
             } else { //no errors
               let statusCode = (response.response?.statusCode)! //example : 200
             }
}
Counterpunch answered 13/10, 2015 at 12:37 Comment(3)
response.result.error can give you an Alamofire error, for example. StatusCodeValidationFailed, FAILURE: Error Domain=com.alamofire.error Code=-6003. It's if you actually want to get HTTP response error it's better to user response.response?.statusCodeEmendation
@KostiantynKoval I agree with you, you've done a proper clarification. I updated my answer for clarityCounterpunch
working for me... {let statusCode = response.response!.statusCode}Lotz
N
56

In the completion handler with argument response below I find the http status code is in response.response.statusCode:

Alamofire.request(.POST, urlString, parameters: parameters)
            .responseJSON(completionHandler: {response in
                switch(response.result) {
                case .Success(let JSON):
                    // Yeah! Hand response
                case .Failure(let error):
                   let message : String
                   if let httpStatusCode = response.response?.statusCode {
                      switch(httpStatusCode) {
                      case 400:
                          message = "Username or password not provided."
                      case 401:
                          message = "Incorrect password for user '\(name)'."
                       ...
                      }
                   } else {
                      message = error.localizedDescription
                   }
                   // display alert with error message
                 }
Namaqualand answered 14/3, 2016 at 23:42 Comment(3)
Hi, will statusCode 200 fall under failure? my request was handled correctly at the server side but the response falls under FailureRugen
@Rugen I don't think 200 is an http error : see #27922037Namaqualand
@Rugen status code 200 is indicating success, if your server returned 200 and the response indicating error (i.e some property called issuccess = false), then you have to handle that in your frontend codeAphasia
S
25
    Alamofire
        .request(.GET, "REQUEST_URL", parameters: parms, headers: headers)
        .validate(statusCode: 200..<300)
        .responseJSON{ response in

            switch response.result{
            case .Success:
                if let JSON = response.result.value
                {
                }
            case .Failure(let error):
    }
Sprung answered 19/2, 2016 at 9:53 Comment(2)
This stimulates api's best practicesPal
URW :) Try to implement Router for your requests. ;)Sprung
K
12

Best way to get the status code using alamofire.

 Alamofire.request(URL).responseJSON {
  response in

  let status = response.response?.statusCode
  print("STATUS \(status)")

}
Kris answered 1/7, 2017 at 4:36 Comment(0)
S
6

Or use pattern matching

if let error = response.result.error as? AFError {
   if case .responseValidationFailed(.unacceptableStatusCode(let code)) = error {
       print(code)
   }
}
Surfacetoair answered 17/3, 2017 at 16:3 Comment(2)
Worked like a charm.Acroterion
Useful when only the Error has been passed to the view, not the responseSemang
G
5

you may check the following code for status code handler by alamofire

    let request = URLRequest(url: URL(string:"url string")!)    
    Alamofire.request(request).validate(statusCode: 200..<300).responseJSON { (response) in
        switch response.result {
        case .success(let data as [String:Any]):
            completion(true,data)
        case .failure(let err):
            print(err.localizedDescription)
            completion(false,err)
        default:
            completion(false,nil)
        }
    }

if status code is not validate it will be enter the failure in switch case

Grewitz answered 18/12, 2017 at 9:50 Comment(0)
M
4

In your responseJSON completion, you can get the status code from the response object, which has a type of NSHTTPURLResponse?:

if let response = res {
    var statusCode = response.statusCode
}

This will work regardless of whether the status code is in the error range. For more information, take a look at the NSHTTPURLResponse documentation.

For your other question, you can use the responseString function to get the raw response body. You can add this in addition to responseJSON and both will be called.

.responseJson { (req, res, json, error) in
   // existing code
}
.responseString { (_, _, body, _) in
   // body is a String? containing the response body
}
Marginal answered 18/3, 2015 at 20:4 Comment(0)
L
3

Your error indicates that the operation is being cancelled for some reason. I'd need more details to understand why. But I think the bigger issue may be that since your endpoint https://host.com/a/path is bogus, there is no real server response to report, and hence you're seeing nil.

If you hit up a valid endpoint that serves up a proper response, you should see a non-nil value for res (using the techniques Sam mentions) in the form of a NSURLHTTPResponse object with properties like statusCode, etc.

Also, just to be clear, error is of type NSError. It tells you why the network request failed. The status code of the failure on the server side is actually a part of the response.

Hope that helps answer your main question.

Latrell answered 18/3, 2015 at 20:47 Comment(4)
Good catch, I was too focused on the list of questions at the bottom.Marginal
Its actually receiving a code 401 Unauthorized because I am intentionally sending the wrong password to simulate a user mistyping their password so that I can catch that case and give the user feedback. Thats not the actual URL I am using, but I am using a legit URL that results in success when I send the correct password. The console output in my original post is actual output from hitting a real URL (except that the URL is fake) and you can see that the res object is nil, so this answer does not help, sorry.Fungi
Thanks for clarifying. Well, then the only thing that's suspect here is the -999 error I haven't encountered that, but documentation suggests that the request is being cancelled (so the question of even receiving a response should be moot). Have you tried to print the response object for a different type of error that's not auth-related? Just curious.Latrell
ahhhh i thought error was referring to responses with status codes that are out of the range we provide for validate(). Thanks!!!Piddle
F
3

I needed to know how to get the actual error code number.

I inherited a project from someone else and I had to get the error codes from a .catch clause that they had previously setup for Alamofire:

} .catch { (error) in

    guard let error = error as? AFError else { return }
    guard let statusCode = error.responseCode else { return }

    print("Alamofire statusCode num is: ", statusCode)
}

Or if you need to get it from the response value follow @mbryzinski's answer

Alamofire ... { (response) in

    guard let error = response.result.error as? AFError else { return }
    guard let statusCode = error.responseCode else { return }

    print("Alamofire statusCode num is: ", statusCode)
})
Foliose answered 26/3, 2020 at 21:18 Comment(0)
Y
2
    AF.request(url, method: .get).responseDecodable(of: Weather.self) { response in
        switch response.result {
        case .success(let data):
            print(data)
            var statusCode = response.response?.statusCode
            
            if statusCode == 200 {
                print(response)
            }
        case .failure(let error):
            print(error)
        }
    }
Yalonda answered 29/12, 2022 at 5:51 Comment(1)
response.response?.statusCode has already been mentioned in several other answers.Saffian
J
1

For Swift 2.0 users with Alamofire > 2.0

Alamofire.request(.GET, url)
  .responseString { _, response, result in
    if response?.statusCode == 200{
      //Do something with result
    }
}
Jaquez answered 17/9, 2015 at 10:50 Comment(0)
E
1

For Swift 3.x / Swift 4.0 / Swift 5.0 users with Alamofire >= 5.0

Used request modifier to increase and decrease the timeout interval.

Alamofire's request creation methods offer the most common parameters for customization but sometimes those just aren't enough. The URLRequests created from the passed values can be modified by using a RequestModifier closure when creating requests. For example, to set the URLRequest's timeoutInterval to 120 seconds, modify the request in the closure.

var manager = Session.default
 manager.request(urlString, method: method, parameters: dict, headers: headers, requestModifier: { $0.timeoutInterval = 120 }).validate().responseJSON { response in

OR

RequestModifiers also work with trailing closure syntax.

var manager = Session.default
     manager.request("https://httpbin.org/get") { urlRequest in
    urlRequest.timeoutInterval = 60
    urlRequest.allowsConstrainedNetworkAccess = false
}
.response(...)
Et answered 13/11, 2020 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.