0

I'm having difficulties to get responseString when server failed to send a valid JSON response (fe. php echos some temp variable or something went wrong). I am using AFJSONRequestOperation from AFNetwoking like this:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject) {
        NSLog(@"object: %@", responseObject);
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseString) {
        NSLog(@"failure: %@", responseString);
    }
];

responseString is case of failure is always nil. When I tried to read the documentation (http://cocoadocs.org/docsets/AFNetworking/1.3.1/Classes/AFJSONRequestOperation.html#//api/name/JSONRequestOperationWithRequest:success:failure:) I've found that there is written that failure gets three arguments (but in reality four, the fourth being always nil). Is there any simple way to get the response as a string in that case?

patryk
  • 642
  • 1
  • 9
  • 18
  • What response are you getting on failure. – icodebuster Aug 02 '13 at 19:28
  • What is the content length indicated by the response object? – Wain Aug 02 '13 at 19:35
  • 2013-08-02 21:06:12.250 app[44769:c07] <0x7b3d720 ISWebServiceManager.m:(79)> responseCode: 200, error: The operation couldn’t be completed. (Cocoa error 3840.) (null) how to get content length from response? – patryk Aug 02 '13 at 19:50
  • 1
    Maybe it's a bit off-topic, but when this happens to me, I always use cURL to test the server request and print the output in the terminal. This helps me to catch the possible echo's or notices. – amb Aug 05 '13 at 07:37

1 Answers1

0

If responseString is nil, then you either didn't receive any data from the server, or the data could not be used to create a valid NSString object.

By the sound of your Cocoa error 3840, which corresponds to an NSJSONSerialization error, my guess is that the server was indeed sending back an empty response.

mattt
  • 19,544
  • 7
  • 73
  • 84
  • unfortunately not whole of your answer is true, JSON serialization fails, but the response is not empty (I even tried putting in webservice something like print('something'); die(); and the responseString is always nil...) – patryk Aug 09 '13 at 11:42
  • @patryk How are you checking the response? Are you using `curl`? Sniffing the incoming HTTP traffic in the simulator using Charles.app, Wireshark, or something similar? – mattt Aug 10 '13 at 23:56
  • I'm using GraphicalHTTPClient (available in appstore) with the same request parameters as AFNetworking call. – patryk Aug 12 '13 at 08:47
  • @patryk Without sniffing traffic, you can't be entirely sure about the particular details of the outgoing request. It's not just parameters: request headers are significant too. I would encourage you to use a more comprehensive tool to validate everything. – mattt Aug 12 '13 at 12:59
  • In that case GraphicalHTTPClient is sufficient, because i know that there is something in response (because when I get proper JSON success method gives proper object and when I type print('something') to spoil the request responseString in failure method is empty...). – patryk Aug 12 '13 at 13:29