Swift Alamofire: So erhalten Sie den HTTP-Antwortstatuscode

Ich möchte den HTTP-Antwortstatuscode (z. B. 400, 401, 403, 503 usw.) für Anforderungsfehler abrufen (und idealerweise auch für Erfolge). In diesem Code führe ich eine Benutzerauthentifizierung mit HTTP Basic durch und möchte dem Benutzer mitteilen, dass die Authentifizierung fehlgeschlagen ist, wenn der Benutzer sein Kennwort falsch eingibt.

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")
}

Leider scheint der erzeugte Fehler nicht darauf hinzudeuten, dass tatsächlich ein HTTP-Statuscode 409 empfangen wurde:

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

Zusätzlich wäre es nett, den HTTP-Body abzurufen, wenn ein Fehler auftritt, da meine Serverseite dort eine Textbeschreibung des Fehlers anbringen wird.

Frage
Ist es möglich, den Statuscode bei einer Nicht-2xx-Antwort abzurufen?
Ist es möglich, den spezifischen Statuscode bei einer 2xx-Antwort abzurufen?
Ist es möglich, den HTTP-Body bei einer Nicht-2xx-Antwort abzurufen?

Vielen Dank

Antworten auf die Frage(18)

Ihre Antwort auf die Frage