Basic Authentication with Alamofire

前端 未结 6 1818
轻奢々
轻奢々 2020-12-15 05:41

Experiencing an issue when authenticating with Basic Auth. I am using a standard enum that conforms to URLRequestConvertible protocol to construct my requests.

6条回答
  •  庸人自扰
    2020-12-15 06:27

    In swift 3.0

    Use following code -

        let user = ***
        let password = ***
        let credentialData = "\(user):\(password)".data(using: String.Encoding.utf8)!
        let base64Credentials = credentialData.base64EncodedString(options: [])
        let headers = ["Authorization": "Basic \(base64Credentials)"]
    
        Alamofire.request(customerURL,
                          method: .get,
                          parameters: nil,
                          encoding: URLEncoding.default,
                          headers:headers)
            .validate()
            .responseJSON { response in
                if response.result.value != nil{                    
                   print(response)
                }else{
    
                }
        }
    

提交回复
热议问题