Reputation: 11
When i try to call post request using nsurlsession cannot able to cache the response.Please help me out.
Thanks in advance
Upvotes: 1
Views: 950
Reputation: 1907
Although dgatwood 's answer does have a point (POST requests are not idempotent and therefore should not be cached), I have to disagree about the behaviour of NSURLCache.
In my experience NSURLCache DOES cache POST requests.
That is evident if you take a look at your Cache.db
file in your application's Library/Caches/< App-BundleID >/
directory. Furthermore the POST cached responses ARE in fact returned by NSURLCache's - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
method.
You can find other possible causes for your responses not being cached in Kumar Saurabh 's comment to your question, or my answer here: NSURLCache: inconsistent behaviour
Upvotes: 2
Reputation: 10417
POST requests are not, according to the HTTP spec, idempotent. Specifically:
For this reason, the NSURL family of APIs refuses to cache POST requests by default.
There are ways to explicitly cache the request by creating a custom NSURLProtocol object. If you go down that path, you must rewrite the URL on its way into the cache to include the POST body data (or some portion thereof) as part of the URL. If you don't, you'll get the same cached response for every POST request to that URL, regardless of the actual POST body contents, which is probably not what you want.
With that said, caching POST requests is almost never the right thing to do. If you want caching, use a GET request.
Upvotes: 2