hgwhittle
hgwhittle

Reputation: 9426

How can I log each request/response using AFHTTPSessionManager?

I'm migrating my project to AFNetworking 2.0. When using AFNetworking 1.0, I wrote code to log each request/response in the console. Here's the code:

-(AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
                                                   success:(void (^)(AFHTTPRequestOperation *, id))success
                                                   failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
{
    AFHTTPRequestOperation *operation =
    [super HTTPRequestOperationWithRequest:request
        success:^(AFHTTPRequestOperation *operation, id responseObject){
             [self logOperation:operation];
             success(operation, responseObject);
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error){
             failure(operation, error);
    }];

    return operation;
}

-(void)logOperation:(AFHTTPRequestOperation *)operation {

    NSLog(@"Request URL-> %@\n\nRequest Body-> %@\n\nResponse [%d]\n%@\n%@\n\n\n",
    operation.request.URL.absoluteString,
    [[NSString alloc] initWithData:operation.request.HTTPBody encoding:NSUTF8StringEncoding],
    operation.response.statusCode, operation.response.allHeaderFields, operation.responseString);
}

I'm trying to do the same thing using AFNetworking 2.0, which to my understanding, means using a NSURLSessionDataTask object instead of AFHTTPRequestOperation. Here's my shot at it.

-(NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *, id, NSError *))completionHandler {

    NSURLSessionDataTask *task = [super dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error){

        [self logTask:task];
        completionHandler(response, responseObject, error);
    }];

    return task;
}

-(void)logTask:(NSURLSessionDataTask *)task {

    NSString *requestString = task.originalRequest.URL.absoluteString;
    NSString *responseString = task.response.URL.absoluteString;

    NSLog(@"\n\nRequest - %@\n\nResponse - %@\n\n", requestString, responseString);
}

The dataTaskWithRequest:completionHandler method is successfully intercepting each call, so I think that's the right method to override, but when I try to log the task in the completionHandler, task is nil. Thus getting nulls printed in the console. However a proper task object is still returned from that method. What's happening here? How can I properly log the request/response for each call?

Upvotes: 1

Views: 7019

Answers (1)

primax79
primax79

Reputation: 428

you can use the library AFNetworking/AFNetworkActivityLogger

https://github.com/AFNetworking/AFNetworkActivityLogger

from the doc:

AFNetworkActivityLogger is an extension for AFNetworking 2.0 that logs network requests as they are sent and received.

usage:

[[AFNetworkActivityLogger sharedLogger] startLogging];

output:

GET http://example.com/foo/bar.json
200 http://example.com/foo/bar.json

using devel logging level you should have responseHeaderFields and responseString too

Upvotes: 6

Related Questions