Note that there are some explanatory texts on larger screens.

plurals
  1. POAFNetworking 500 response body
    text
    copied!<p>I've been using AFNetworking 2.0 in my app. I've noticed that if my web-service returns a 500 status code I do not get the body of the response.</p> <p>Here is an example of my php code</p> <pre><code>try { $conn = new PDO( "sqlsrv:server=$serverName;Database = $database", $uid, $pwd); $conn-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); return $conn; } catch( PDOException $e ) { $response-&gt;status(500); echo( "Connection Error: " . $e-&gt;getMessage() ); } </code></pre> <p>If I use a simple rest client this is an example of a response body.</p> <pre><code>Connection Error: SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. </code></pre> <p>However this seems to be the only response I can get from AFNetworking</p> <pre><code>Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x15e58fa0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} </code></pre> <p>This is the part of my objective-c code that does this.</p> <pre><code>...} failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"%@",error.description); }]; </code></pre> <p>Is there a way I can get the response body?</p> <p>Edit: More code for clarification</p> <p>Below is part of my subclass of AFHTTPSessionManager</p> <pre><code>@implementation MSMAMobileAPIClient + (MSMAMobileAPIClient *)sharedClient { static MSMAMobileAPIClient *_sharedClient = nil; static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ _sharedClient = [[MSMAMobileAPIClient alloc] initWithDefaultURL]; }); return _sharedClient; } - (id)initWithDefaultURL { return [self initWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://%@/mamobile/index.php/" ,[[NSUserDefaults standardUserDefaults] stringForKey:@"serviceIPAddress"]]]]; } - (id)initWithBaseURL:(NSURL *)url { self = [super initWithBaseURL:url]; if (!self) { return nil; } self.responseSerializer = [AFCompoundResponseSerializer compoundSerializerWithResponseSerializers:@[[AFJSONResponseSerializer serializer], [AFHTTPResponseSerializer serializer]]]; return self; } </code></pre> <p>I tried setting the response serializer to a AFCompoundResponseSerializer but it didn't seem to make a difference</p> <p>Below is an example of a subclass that I call the Librarian.</p> <pre><code>-(void)searchForItemWithString:(NSString *)searchString withCompletionBlock:(arrayBlock)block { self.inventorySearchBlock = block; NSDictionary *parameters = @{@"query": searchString}; [[MSMAMobileAPIClient sharedClient] GET:@"inventory/search" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { if (!responseObject) { NSLog(@"Error parsing JSON"); } else { //do stuff with the json dictionary that's returned.. } } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"Error: %@",error.description); }]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload