Jonathan Thurft Jonathan Thurft - 1 year ago 62
Objective-C Question

How to make AFNetworking show plain HTTP response error

I'm trying to send some information from my app to my PHP server side program.

When I try to send the same data using the

PHP Array
as UnitTest it works. So I dont know why AFNeworking is thinking that this doesnt work.

In order to debug the problem i need to be able to see the full HTTP message.

How could I see the plain(NON-JSON) error (the HTTP response)?

I though that
operation.JSONReadingOptions = NSJSONReadingAllowFragments;
would do the trick, but is not working or i am not placing it in the right place


My error:

2013-06-05 10:52:37.990 iGym[9407:c07]Request failed with error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x96db4d0 {NSDebugDescription=Garbage at end.}, {
NSDebugDescription = "Garbage at end.";


My Objective-c code

User* myUser = [self getCurrentUser];
NSURL *url = [[NSURL alloc]initWithString:@""];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];

NSDictionary *params = @{@"register":@"true",
@"userID":(myUser.idUserExternal ? myUser.idUserExternal: @"NO"),
@"userDetails":@{@"needUsername":(myUser.nickname ? @"NO": @"YES"),
@"username":( myUser.nickname ? myUser.nickname : _input.text),
@"language":(myUser.language? myUser.language: @"0"),
@"name":( ? : @"0"),
@"surname":(myUser.surname ? myUser.surname : @"0"),
@"country":( ? : @"0"),
@"dob":(myUser.dob ? myUser.dob : @"0"),
@"city":( ? : @"0"),
@"height":(myUser.height ? myUser.height : @"0"),
@"weight":(myUser.weight ? myUser.weight : @"0"),
@"metricType":(myUser.metricSystem ? myUser.metricSystem : @"0")
NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"igym/bootstrap.php" parameters:params];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSLog(@"Inside the success block %@",JSON);
if ([JSON objectForKey:@"ok"]) {
[self done:JSON];

if ([JSON objectForKey:@"error"]) {
[self tryAgain:JSON[@"error"]];
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){

NSLog(@"json text is: %@", JSON);
NSLog(@"Request failed with error: %@, %@", error, error.userInfo);
operation.JSONReadingOptions = NSJSONReadingAllowFragments;

[operation start];

Answer Source

In your success or error block, you can NSLog operation.responseString or operation.responseData. That will give you the raw string or bytes sent from the server.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download