[英]How to make a request using Token in the header and basic authentication in Alamofire?
我需要向自己的API發出請求,后端人員在發出請求時需要基本身份驗證,而且我還必須將令牌放在標頭中。 向API提出請求時,我正在使用Alamofire。
這是郵遞員的基本身份驗證
這是標題中的X-API-KEY令牌
但我不知道如何在標頭中同時實現基本身份驗證和X-API-KEY令牌。 以前,如果僅使用下面的代碼使用基本身份驗證,我就可以發送請求,但是我不知道如何在使用alamofire進行請求時同時要求它們(標頭中的基本身份驗證和X-API-KEY令牌)如何發出請求;
let urlSendDefect = URLService.defects.endPoint
let username = "admin"
let password = "1234"
var headers: HTTPHeaders = [:]
if let authorizationHeader = Request.authorizationHeader(user: username, password: password) {
headers[authorizationHeader.key] = authorizationHeader.value
}
let parameters : [String:Any] = ["defect_id": defectID, "defect_comment" : comment, "status" : status]
Alamofire.request(urlSendDefect,
method: .put,
parameters: parameters,
encoding: URLEncoding.default,
headers:headers)
.validate()
.responseJSON { response in
switch response.result {
case .failure(let error) :
print("Error while making request to send defect comment to server: \(error.localizedDescription)")
completion(nil,error)
case .success(let value) :
let json = JSON(value)
if let message = json["message"].string {
if message.isEmpty {
completion(nil,nil)
} else {
completion(message,nil)
}
} else {
completion(nil,nil)
}
}
}
你能嘗試像這樣構造它嗎
let headers : HTTPHeaders = ["Content-Type":"application/json",
"Authorization":"Basic \(Your_token)"]
並添加您想要的任何其他鍵值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.