POST Request returning unwanted cached results (AFNetworking)

afnetworking, caching, ios, post

Solution

Try instead

request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

because the `NSURLRequestReloadIgnoringCacheData` only ignores local cache data and not caches out on the network.

Edit: As Steve Madsen points out below, this was not the real problem, and, in general, responses to POST requests are not cached in any case. The actual problem was that the program didn't log out between two logins, by mistake. But we did fix it in the end!

Problem

I'm using AFNetworking to make POST requests from a shared "authenticator" class that passes in a user's username and password. Here is the POST request that I make: ``` NSURL *url = [NSURL URLWithString:@"https://www..."]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; // params NSDictionary* dict = @{@"loginName": username, @"password": password, @"serviceName": @"...", @"serviceURL": @"...", @"action": @"..."}; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; request = [httpClient requestWithMethod:@"POST" path:@"..." parameters:dict]; request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request.timeoutInterval = 30.0; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"%@", operation.responseString); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"login failed"); }]; [operation start]; ``` It works very well for the first login and everything returns as expected. When I attempt to login with a different username/password, I see that the output of the `operation.responseString` is the exact same output as the first login. Does anyone know why it is returning the output from the first login? I feel that the response is a cached response and I had added the following to try to prevent the return of cached information: ``` request.cachePolicy = NSURLRequestReloadIgnoringCacheData; ``` I have set breakpoints to see that the username and password in the `NSDictionary` for the parameters are the new username/password combination. The string literals are not manipulated in anyway as well and are the same in every POST request. The elipses are for privacy and are placeholders for strings with semantic meaning.

Original source

Related problems