Skip to content

Network Debug Tool

Mengyan Wang edited this page Sep 15, 2015 · 7 revisions

Parse network debugging tool is a powerful tool which allows you to monitor the requests and responses between Parse SDK and Parse Server. In iOS/OSX, we use the notification mechanism to expose the NSURLRequest, NSURLResponse and the response body to you. You can follow this tutorial to use these notifications in you app.

Set Parse log level

Set the Parse log level to debug in your AppDelegate.

@implementation AppDelegate

- (void)application:(UIApplication *)application didFinishLaunchWithOptions:(NSDictionary *)options {
    [Parse setLogLevel:PFLogLevelDebug];
    ....
}

@end

Declare notification selectors

Declare the notification selectors in your AppDelegate. These methods are used to monitor the requests and responses when we get the PFCommandRunnerWillSendURLRequestNotification and PFCommandRunnerDidReceiveURLResponseNotification notifications. For example, the following two methods log request and response information to console. All the const string we use in the following codes can be found in PFConstants.h.

@implementation AppDelegate

- (void)receiveWillSendURLRequestNotification:(NSNotification *) notification {
  NSURLRequest *request = notification.userInfo[PFNetworkNotificationURLRequestUserInfoKey];
  NSLog(@"URL : %@", request.URL.absoluteString);
  NSLog(@"Method : %@", request.HTTPMethod);
  NSLog(@"Headers : %@", request.allHTTPHeaderFields);
  NSLog(@"Request Body : %@", [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]);
}

- (void)receiveDidReceiveURLResponseNotification:(NSNotification *) notification {
  NSURLRequest *request = notification.userInfo[PFNetworkNotificationURLRequestUserInfoKey];
  NSHTTPURLResponse *response = notification.userInfo[PFNetworkNotificationURLResponseUserInfoKey];
  NSString *responseBody = notification.userInfo[PFNetworkNotificationURLResponseBodyUserInfoKey];
  NSLog(@"URL : %@", response.URL.absoluteString);
  NSLog(@"Status Code : %ld", (long)response.statusCode);
  NSLog(@"Headers : %@", response.allHeaderFields);
  NSLog(@"Response Body : %@", responseBody);
}

@end

Register to notification center

Add monitor methods to the NSNotificationCenter.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveWillSendURLRequestNotification:) name:PFCommandRunnerWillSendURLRequestNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveDidReceiveURLResponseNotification:) name:PFCommandRunnerDidReceiveURLResponseNotification object:nil];

Monitor

After the setup, when Parse SDK communicates with Parse server, the related monitor methods will be triggered. Fox example, for the two monitor methods above, you can see the requests and responses in the console like this:

Clone this wiki locally