While using a 3rd party API, I have the requirement to cancel all traffic when a custom response header is set to a certain value. I am trying to find a nice place to do this check only once in my code (and not in every success/failure block, where it works fine). From what I understand, this could be done by overriding -(void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation in my custom AFHTTPClient subclass, but when I implement it like that:
-(void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation
{
NSLog(@"[REQUEST URL]\n%@\n", [operation.request.URL description]);
NSLog(@"[RESPONSE HEADERS]\n%@\n", [[operation.response allHeaderFields] descriptionInStringsFileFormat]);
[super enqueueHTTPRequestOperation:operation];
}
the response headers are nil. Can anybody help me with that?
At the moment when operations are being created and enqueued in
AFHTTPClient, they will not have the response from the server–that will be assigned when the request operation is actually executed.Although the requirement to cancel all traffic seems unorthodox (at least if outside of the conventions of HTTP), this is easy to accomplish:
In your
AFHTTPClientsubclass, add aBOOLproperty that stores if requests should be prevented, and then used inenqueueHTTPRequestOperation. Then, overrideHTTPRequestOperationWithRequest:success:failure:to execute the specified success block along with some logic to set the aforementioned property if the salient response is present.