I've created a network layer using Moya, however for one of my requests I'm passing a etag in the If-None-Match header and even though this correspond to the returning Etag it still return a JSON array, even though I've tested it in Postman where it does not return anything. I guess this is due to the fact that cache-control is turned off? How can I achieve this?
let endpointClosure = { (target: API) -> Endpoint<API> in
let endpoint = Endpoint<API>(URL: url(target), sampleResponseClosure: {.networkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)
switch target {
case .auth(let fbAccessToken):
return endpoint.adding(newHttpHeaderFields: [
"x-access-token": "\(fbAccessToken)"
])
case .games(let accessToken, let etag):
return endpoint.adding(newHttpHeaderFields: [
"x-access-token": "\(accessToken)",
"If-None-Match": "\(etag)"
])
}
}
Correct me if I'm wrong, but isn't this the way that etags are supposed to work? The networking layer should, if it gets a 304 from the server, just return the cached version of it. Which seems like what you're saying it is doing? So Postman will give you a 304 because it's saying there's no update, and Alamofire is seeing the 304 and just returning the cached value. Seems like the intended functionality unless I am misunderstanding you, or how etags work ;)
Your correct however at the moment if i pass a etag in the If-None-Match i get an array back even though when i check the response the etag is exactly the same i passed in If-None-Match. If these are the same it is suppose to give 304 error, but its not.
@Fredehagelund92 this sounds like an issue at the Alamofire level. Moya doesn't actually handle making the request, all of that work is delegated to Alamofire. You may be better suited to asking the folks over there?
Apologies if I misunderstood your problem, and the issue is actually something related to Moya incorrectly embedding your headers 馃槄
As this seems to be an Alamofire issue, I'll close this for now. Please reopen if not!
This is actually the default cache policy in iOS when using an URLRequest, which is used by Alamofire at a lower level.
If you want to change this behavior while using Moya and see the "real" response to your request, you just need to:
requestClosure, where you can modify the request before it's given to Alamofire to hit the network. Here you can change the URLRequest cache policy.let requestClosure = { (endpoint: Endpoint<YourTarget>, done: MoyaProvider.RequestResultClosure) in
var request: URLRequest = endpoint.urlRequest!
// this is the important line
request.cachePolicy = .reloadIgnoringCacheData
done(.success(request))
}
MoyaProvider using this parameter:let provider = MoyaProvider<YourTarget>(requestClosure: requestClosure)
And you're done!
You can still define the MoyaProvider using both requestClosure and endpointClosure:
let provider = MoyaProvider<YourTarget>(endpointClosure: endpointClosure, requestClosure: requestClosure)
Hope this helps someone!
@davidecastello lifesaver! Great stuff
@davidecastello what a champion! 馃弳
@davidecastello thanks, fixed a problem we had on production
Thanks so much @davidecastello ! I was very confused by having my 304's automagically turned into 200's by URLSession.
Most helpful comment
This is actually the default cache policy in iOS when using an
URLRequest, which is used by Alamofire at a lower level.If you want to change this behavior while using Moya and see the "real" response to your request, you just need to:
requestClosure, where you can modify the request before it's given to Alamofire to hit the network. Here you can change theURLRequestcache policy.MoyaProviderusing this parameter:And you're done!
You can still define the
MoyaProviderusing bothrequestClosureandendpointClosure:Hope this helps someone!