Using Alamofire, is it possible to have a function to handle the header response before downloading the complete file?
For example:
Our app uses the same elements on multiple pages. These elements are collected using request. Each request has his own hash (md5 checksum). We are sending this hash in the headers & i want to abort the request if the hash is recognised in the cache system.
Example implementation
APIManager.sharedManager.request(url, method: method, parameters: parameters)
.doSomethingHere {
//I want to read the headers here, before the data is fetched from the server.
//There needs to be an option here to cancel the request.
}
.responseJSON { response in
//If the request isn't cancel in the function above. The data should be here.
}
}
Edit: Solution (Alamofire implementation SWIFT 3)
APIManager.sharedManager.delegate.dataTaskDidReceiveResponse =
{(session:URLSession, dataTask:URLSessionDataTask, response:URLResponse) -> URLSession.ResponseDisposition in
if let httpResponse = response as? HTTPURLResponse {
//Do something with headers here. If you don't want to continue the request:
return URLSession.ResponseDisposition.cancel
}
return URLSession.ResponseDisposition.allow
}
APIManager.sharedManager.request(url, method: method, parameters: parameters)
.responseJSON { response in
//Response contains no data if it was canceled.
}
}