Search code examples
iosios7afnetworking-2

how to get raw JSON response from AFnetworking 2.0 operation / NSURLSessioNDataTask?


This is my code.. its a very simple operation

[self GET:operationName parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
          NSLog(@"%@", responseObject);
          //do something upon success
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
         //do something to handle error
    }];

My question is, I need to see what the exact raw json response is.. when I NSLog the responseObject, it's not the same JSON output that I would get from a standalone HTTP client _ I guess its because it's been through the serializer?


Solution

  • If you don't want it to do the NSJSONSerialization conversion to NSArray/NSDictionary, but rather want the raw NSData, you should set the responseSerializer of the AFURLSessionManager to a AFHTTPResponseSerializer.

    self.responseSerializer = [AFHTTPResponseSerializer serializer];
    

    The default value is AFJSONResponseSerializer. If you don't want it to convert the JSON for you, change the response serializer.