根据基本身份验证架构,http标头的格式应为Authentication: Basic name:password
,以与url发送到服务器端。
假设我有一个简单的登录功能。
func login() {
guard let url = URL(string: "http://0.0.0.0:5000/v1/token") else {
print("Invalid url")
return
}
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// What's the exactly step to setup the basic authentication
// request.httpMethod = "PUT"
// request.addValue("Basic \(name):\(pass)", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let decodeResponse = try? JSONDecoder().decode(Token.self, from: data) {
DispatchQueue.main.async {
self.token = decodeResponse.token
}
return
}
}
print("Fetch failed")
}
}
我应该如何设置基本身份验证步骤?