Search code examples
iosobjective-cxcodeafnetworkingafnetworking-2

Can i send an entire JSON object using post method in AFNetworking


Is it possible to send a JSON object to web service (RESTful) using AFNetworking AFHTTPRequestOperationManager method post ? I know we can send key value pairs though dictionary and that can be serialized as a JSON through POST request but this about more complex object as follows

{
  "User":{ "name":"blah", "id":"blah blah" }
  "department":"something" 
}

The code i was trying was this

 NewUser *user = [NewUser sharedNewUser];

     NSDictionary *params = @ {@"FirstName" :user.strFname, @"LastName" :user.strLname,@"Email":user.strEmail,@"PhoneNumber":user.strNum,@"Password":user.strPwd,@"Token":@""};

    NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc] init];
    [jsonDic setValue:params forKey:@"Client"];

    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"URL"]];


    [client postPath:@"chauffr_services/servicestack/RegisterClient?format=json" parameters:jsonDic
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSLog(@"%@",responseObject);
                 NSDictionary *json = responseObject;


             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

             }];

Solution

  • I was trying to send object to service but the jSON wasn't reaching the RESTful service , what i was missing is following line

    [httpclient setParameterEncoding:AFJSONParameterEncoding];