How to print AFNetworking request as RAW data
Solution 1
Built-in AFNetworking tools
For AFNetworking 1.x, use AFHTTPRequestOperationLogger
.
For AFNetworking 2.x, use AFNetworkActivityLogger
.
These tools both use the NSNotification
broadcast by AFNetworking to log request and response data to the console. The amount of information to be displayed is configurable, and they can be configured to ignore certain operations.
Examination in Xcode without these tools
HTTP Requests (outgoing data)
If you want to examine the body of an outgoing request, look at the NSURLRequest
's HTTPBody
parameter, which is a property on your AFHTTPRequestOperation
.
For example, in the method -[AFHTTPClient getPath:
parameters:
success:
failure:]
, after the request is made, you can type this into the debugger:
po [[NSString alloc] initWithData:request.HTTPBody encoding:4]
4 is NSUTF8StringEncoding
, as defined in NSString.h.
The NSURLRequest
's HTTPMethod
parameter provides the method (GET, POST, PUT, etc.) as an NSString
.
HTTP Responses (incoming data)
When your server responds, your success completion block is passed an AFHTTPRequestOperation
object (called operation
by default). You can:
-
p (int)[[operation response] statusCode]
to see the status code -
po [[operation response] allHeaderFields]
to see the headers -
po [operation responseString]
to see the response body -
po [operation responseObject]
to see the response object (which may benil
if it couldn't be serialized)
Solution 2
As of AFNetworking 2.0, you should use AFNetworkActivityLogger
#import "AFNetworkActivityLogger.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef DEBUG
[[AFNetworkActivityLogger sharedLogger] startLogging];
[[AFNetworkActivityLogger sharedLogger] setLevel:AFLoggerLevelDebug];
#endif
return YES;
}
If you are using 3.0
and using CocoaPods, you will also need to pull AFNetworkActivityLogger
from the appropriate branch:
pod 'AFNetworkActivityLogger', git: 'https://github.com/AFNetworking/AFNetworkActivityLogger.git', branch: '3_0_0'
Solution 3
You should have a look at https://github.com/AFNetworking/AFHTTPRequestOperationLogger with AFLoggerLevelDebug as level of debugging.
#import "AFHTTPRequestOperationLogger.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef DEBUG
[[AFHTTPRequestOperationLogger sharedLogger] startLogging];
[[AFHTTPRequestOperationLogger sharedLogger] setLevel:AFLoggerLevelDebug];
#endif
return YES;
}
@end
Solution 4
For AFNetworking 3.0 to be able to set the level of logging, you need the following:
#import <AFNetworkActivityLogger/AFNetworkActivityLogger.h>
#import <AFNetworkActivityLogger/AFNetworkActivityConsoleLogger.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
AFNetworkActivityConsoleLogger *logger = [AFNetworkActivityLogger sharedLogger].loggers.anyObject;
logger.level = AFLoggerLevelDebug;
[[AFNetworkActivityLogger sharedLogger] startLogging];
return YES;
}
Related videos on Youtube
Comments
-
Jacek Kwiecień over 4 years
For debuging purposes I'd want to print whole request body. I'm using AFHTTPClient. printing client gives some information, like headers but post/get params are not there.
IS there a way to do it?
-
Aaron Brager almost 11 yearsThis is great. I think this is a better choice over my answer if you'll need to do regular HTTP debugging.
-
Thomas Besnehard about 10 yearsthere is a new logger for AFNetworking 2.O : github.com/AFNetworking/AFNetworkActivityLogger
-
learner almost 10 yearsI see a great number of up votes: will someone please explain to me how to implement the
HTTP Requests (outgoing data)
portion? I am new to iOS so a bit more detail perhaps. thanks. -
Resty over 8 yearsAny solutions for logging/debugging in AFNetworking 3.x ?