RNCachingURLProtocol icon indicating copy to clipboard operation
RNCachingURLProtocol copied to clipboard

POST data to API (Django

Open alexvenom opened this issue 11 years ago • 1 comments

For some reason, when using RNCachingURLProtocol together with AFNetworking, all my POST requests (JSON) to the API by AFNetworking stopped working. Took me a while to find out that RNCachingURLProtocol was interfering with it at some point. Once I removed RNCachingURLProtocol, the API started working again. All my POST requests were getting to the API as GET requests.

This is the code I was using:

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init]; [manager setRequestSerializer:[AFJSONRequestSerializer serializer]]; [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

[manager POST:serverURL parameters:parametersDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];

alexvenom avatar Apr 01 '14 14:04 alexvenom

RNCachingURLProtocol explicitly does not cache POST requests (or anything but GET requests). It's sample code to demonstrate how to build your own NSURLProtocol. It is typically configured by rewriting the parts that you want changed.

That said, this would not be a bad pull request. You just need to check that HTTPMethod is GET in canInitWithRequest: and return NO if it isn't. That would be a reasonable default behavior.

rnapier avatar Apr 24 '14 00:04 rnapier