I am building an app that has a sign in using http basic authentication. If the credentials are correct, it works fine. However for invalid credentials, I cannot access the status code (to test for 401) and the errors localizedDescription shows: cancelled
generated online
Log in using HTTP Basic auth with incorrect credentials
pass the status code even if there is a failure.
@dylan-colaco am I correct in saying that you need access to the status code of the HTTP response to determine the next action in your app (e.g. ask users to try again)?
cc @jaz-ah @Edubits
Yes, that is correct. As far as I can see, the status code of the HTTP response is accessible only for a successful response at the moment, not for unsuccessful responses(like 401).
in my codebase I'm adding this at the bottom - that helps this issue and I'm gonna make a PR to bring into this tree shortly.
private func processRequest(request: Request, _ managerId: String, _ completion: (response: Response<T>?, error: ErrorType?) -> Void) {
if let credential = self.credential {
request.authenticate(usingCredential: credential)
}
request.responseJSON(options: .AllowFragments) { response in
managerStore.removeValueForKey(managerId)
// AlamoFire doesn't automatically check status responses for error codes
// so we do it manually here - also we could call validate() on the AlamoFire
// request but it doesn't put the status code in the error message
// which isn't too helpful in the client code
if request.response?.statusCode >= 400 {
let error = NSError(domain: (request.response?.URL?.URLString)!, code: (request.response?.statusCode)!, userInfo: nil)
error.log()
completion(response: nil, error: error)
return
}
Most helpful comment
in my codebase I'm adding this at the bottom - that helps this issue and I'm gonna make a PR to bring into this tree shortly.