1

having an odd issue with AFNetworking recently. I have a PHP backend and I'm using the SLIM framework. Simplified example of what is happening: If I use the link http://xxx.xxx.xx.xx/InstaAPI/hi this should be called:

$app->get('/hi', function() use($app) { 

    $app->response->setStatus(200);
    echo "hiiii\n";
});

Now in my objective-c code I have:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://xxx.xxx.xx.xx/InstaAPI/hi" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSLog(@"ok");
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"fail");
        NSLog(@"%@", operation.responseString);
    }];

The result I'm getting in the output console is:

015-10-08 18:30:20.650 iReporter[12822:3214201] fail
2015-10-08 18:30:20.650 iReporter[12822:3214201] hiiii

Have no idea why it's calling the failure block. The status is after all set to 200 so it should be okay. Could someone give me some pointers to what I might be doing wrong here please?

Lorenzo
  • 3,293
  • 4
  • 29
  • 56
Kex
  • 8,023
  • 9
  • 56
  • 129
  • Have you tried logging out the error from the fail block? It might shed some more light on this – TommyBs Oct 08 '15 at 10:48
  • ahh thanks man. Bit of a newb so didn't think checking like that. There was a problem because the headers are set to JSON. If you see my echo "hiiii\n"; that isn't a JSON response. For some reason I thought I could debug using echoes scattered about. Obviously can't use them all if it's set to JSON. – Kex Oct 08 '15 at 11:06

1 Answers1

1

Going to answer this myself. It turned out to be very simple. My headers are set to 'Content-Type', 'application/json'. The line echo "hiiii\n"; is not a JSON, so I had to use:

$response = array("Response"=>"HI!");
$echo json_encode($response);

The reason I was getting errors like this was because I was trying to debug the code by scattering echoes in stupid places to check where execution had got to in my PHP script. Moral of the story, don't use a single echo if your headers are set to JSON!

Kex
  • 8,023
  • 9
  • 56
  • 129