我想通过我已经拥有的Bearer进行身份验证,但它始终返回[“error”:Unauthenticated。]。如果我使用相同的标题和Bearer与Postman我得到了我期待的。知道为什么我的标题不起作用吗?
private func getAllData() {
guard let url = URL(string: "http://afit.herokuapp.com/api/user/") else { return }
let request: URLRequest = createRequestForData(withURL: url)
for i in request.allHTTPHeaderFields! {
print(i)
print("\n")
}
DownloadsManager.sharedInstance.getJSONData(fromURLRequest: request) { (responseDictionary) in
print(responseDictionary)
}
}
private func createRequestForData(withURL url: URL) -> URLRequest {
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: UserHeader.Accept.rawValue)
request.addValue("application/json", forHTTPHeaderField: UserHeader.Content_type.rawValue)
request.addValue("Bearer \(bearer!)", forHTTPHeaderField: UserHeader.Authorization.rawValue)
return request
}