我的服务器正在使用CORS。当用户成功登录时,响应中包含标题:access-token, uid, client
服务器响应标头包括:Access-Control-Expose-Headers:access-token, uid, client
但是,当我从uploadTask获得成功的响应,并且访问allHeaderFields
时,这些键/值都会丢失。
访问这些标头需要做什么?
谢谢!
编辑添加现在正常运行的客户端代码:
func postReq(url: URL) -> URLRequest{
var request: URLRequest = URLRequest.init(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "content-type")
return request
}
func login(){
let url:URL = baseEndpoint.appendingPathComponent(Endpoints.login.rawValue)
let request: URLRequest = postReq(url: url)
let body: [String : String] = ["email" : "test@test.com", "password": "loremipsum"]
let bodyData:Data = try! JSONSerialization.data(withJSONObject: body)
uploadTask = defaultSession.uploadTask(with: request, from: bodyData, completionHandler: { (responseData, response, error) in
if(error == nil){
let headers = (response as! HTTPURLResponse).allHeaderFields
}
})
uploadTask?.resume()
}
ANNNND解决了我的问题。没有问题,我只是错过了正确的内容类型。捂脸。