uploadTask allHeaderFields中缺少标题。不包含来自Access-Control-Expose-Headers

时间:2017-05-02 18:36:20

标签: ios swift cors

我的服务器正在使用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解决了我的问题。没有问题,我只是错过了正确的内容类型。捂脸。

0 个答案:

没有答案