Search code examples
jsonhttp-postafnetworking-2

Invalid type in JSON write (NSConcreteMutableData)


I am trying to send a JSON request with AFNetworing but following code giving me JSON text did not start with array or object and option to allow fragments not set error:

NSString *post = [[NSString alloc] initWithFormat:
                  @"{\"request\":\"login\",\"userName\":\"%@\",\"password\":\"%@\"}", userName, password];

NSData *parameters = [post dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *parameterDictionary =
[NSJSONSerialization JSONObjectWithData:parameters options:NSJSONReadingAllowFragments error:nil];

DDLogDebug(@"Data: %@", [parameterDictionary description]);

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:parameterDictionary
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          DDLogDebug(@"LoginView - Success Response: %@", responseObject);
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          DDLogError(@"LoginView - Error Response: %@", [error description]);
      }];

[operation start];

This is the log output of the parameterDictionary object:

{
    password = q;
    request = login;
    userName = q;
}

I have looked similar questions for the error and tried to put parameters object in to an array but this time I got the error "Invalid type in JSON write (NSConcreteMutableData)"

NSMutableArray *array = [NSMutableArray new];
[array addObject:parameterDictionary];

DDLogDebug(@"Data: %@", [parameterDictionary description]);

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:array
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          DDLogDebug(@"LoginView - Success Response: %@", responseObject);
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          DDLogError(@"LoginView - Error Response: %@", [error description]);
      }];

What I am doing wrong?

UPDATE:

I have tried following but it did't work:

NSMutableDictionary *dictionary = [NSMutableDictionary new];
[dictionary setObject:@"login" forKey:@"request"];
[dictionary setObject:@"q" forKey:@"userName"];
[dictionary setObject:@"q" forKey:@"password"];

DDLogDebug(@"Dictionary: %@", [dictionary description]);
DDLogDebug(@"Json: %@", [dictionary JSONRepresentation]);

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];

AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:dictionary
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          DDLogDebug(@"LoginView - Success Response: %@", responseObject);
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          DDLogError(@"LoginView - Error Response: %@", [error description]);
      }];

UPDATE 2:

This what my AFHTTPRequestOperation look like on error block:

<AFHTTPRequestOperation: 0x7fd881fa1200, state: isFinished, cancelled: NO request: <NSMutableURLRequest: 0x7fd881f9fd60> { URL: http://www.olcayertas.com/services.php }, response: <NSHTTPURLResponse: 0x7fd881d913b0> { URL: http://www.olcayertas.com/services.php } { status code: 200, headers {
    Connection = close;
    "Content-Length" = 1050;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 21 Jan 2015 10:28:45 GMT";
    Server = Apache;
    "X-Powered-By" = PleskLin;
} }>

Solution

  • I have solved the problem by checking my web service in browser. The problem was in my services.php file. There was an error about log file creation and this error was returning a non JSON response that causing the request to fail. My complate working code is here:

    NSMutableDictionary *dictionary = [NSMutableDictionary new];
    [dictionary setObject:@"login" forKey:@"request"];
    [dictionary setObject:@"q" forKey:@"userName"];
    [dictionary setObject:@"q" forKey:@"password"];
    
    DDLogDebug(@"Dictionary: %@", [dictionary description]);
    DDLogDebug(@"Json: %@", [dictionary JSONRepresentation]);
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
    [manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
    //[manager.securityPolicy setAllowInvalidCertificates:true];
    
    AFHTTPRequestOperation *operation =
    [manager POST:WEB_SERVICE_URL parameters:dictionary
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              DDLogDebug(@"LoginView - Success Response: %@", responseObject);
          }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              DDLogError(@"LoginView - Error Response: %@", [error description]);
              DDLogError(@"Error: %@",operation);
          }];
    
    [operation start]
    

    And here is my complate services.php file to make it as a complate example for passing and getting JSON with AFNetworiking and PHP service:

    PHP web service that accepts JSON input and return JSON response