Vivere un problema durante l'autenticazione con l'autenticazione di base. Sto usando un enum standard conforme al protocollo URLRequestConvertible
per costruire le mie richieste. Il problema è che quando ho impostato manualmente le intestazioni di autorizzazione nel enum in questo modo:Autenticazione di base con Alamofire
let user = ***
let password = ***
let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
mutableURLRequest.setValue("Basic \(base64Credentials)", forHTTPHeaderField: "Authorization")
Ho sempre trovato una risposta non autorizzati 401. Tuttavia se impostare la password utilizzando il authenticate
richiamata in questo modo:
Alamofire.request(request)
.authenticate(user: "USERNAME_HERE", password: "PASSWORD_HERE")
.responseJSON { (response) -> Void in
print("JSON response \(response)")
completion(success: true, error: nil)
}
Autentica correttamente. Vorrei essere in grado di impostarlo manualmente nell'enum conforme a URLRequestConvertible
anziché passare le credenziali in authenticate
.
So che sta usando un NSURLCredential
sotto il cofano per sfide autentiche, ma mi piacerebbe poterlo impostare manualmente.
Ecco il mio URLRequestConvertible
implementazione:
enum CheckedUpAPI: URLRequestConvertible {
static let baseURLString = "https://***"
static let APIKey = "***"
static let APIClientName = "iPad"
case UpdatePatient(String, [String: AnyObject])
var method: Alamofire.Method {
switch self {
case .UpdatePatient:
return .PATCH
}
}
var path: String {
switch self {
case .UpdatePatient(let patientID, _):
return "patients/\(patientID)"
}
}
// MARK: URLRequestConvertible
var URLRequest: NSMutableURLRequest {
let URL = NSURL(string: CheckedUpAPI.baseURLString)!
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
mutableURLRequest.HTTPMethod = method.rawValue
/**
We are not setting any authorization headers since they requests return 401
the `authenticate` function on Alamofire.request does the trick
let user = "[email protected]"
let password = "test"
let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
mutableURLRequest.setValue("Basic \(base64Credentials)", forHTTPHeaderField: "Authorization")
*/
mutableURLRequest.setValue(CheckedUpAPI.APIKey, forHTTPHeaderField: "API-Key")
switch self {
case .UpdatePatient(_, let parameters):
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
}
}
}
Si prega di condividere come si sta facendo la richiesta di utilizzare il '' mutableURLRequest header usando Alamofire –
@VictorSigler Fatto. L'impostazione manuale delle intestazioni è commentata poiché non ha mai funzionato. ma questo è quello che ho provato e non funzionerebbe quando si utilizza la funzione 'authenticate' da' Alamofire' –
@VictorSigler qualche idea? –