ios - RestKit POST object to Python -
i using restkit ios perform post python (flask) server. post arguments set of nested dictionaries. when create hierarchical argument object on client side , perform post, there no errors. on server side, form data flattened single set of keys indexed strings:
@interface testargs : nsobject @property (strong, nonatomic) nsdictionary *a; @property (strong, nonatomic) nsdictionary *b; @end rkobjectmapping *requestmapping = [rkobjectmapping requestmapping]; // objectclass == nsmutabledictionary [requestmapping addattributemappingsfromarray:@[ @"a.name", @"a.address", @"a.gender", @"b.name", @"b.address", @"b.gender", ]]; rkrequestdescriptor *requestdescriptor = [rkrequestdescriptor requestdescriptorwithmapping:requestmapping objectclass:[testargs class] rootkeypath:nil]; rkobjectmanager *manager = [rkobjectmanager managerwithbaseurl:[nsurl urlwithstring:@"http://localhost:5000"]]; [manager addrequestdescriptor:requestdescriptor]; nsdictionary *a = [nsdictionary dictionarywithobjectsandkeys: @"alexis", @"name", @"boston", @"address", @"female", @"gender", nil]; nsdictionary *b = [nsdictionary dictionarywithobjectsandkeys: @"chris", @"name", @"boston", @"address", @"male", @"gender", nil]; testargs *targs = [[testargs alloc] init]; targs.a = a; targs.b = b; [manager postobject:targs path:@"/login" parameters:nil success:nil failure:nil];
on server side, post body this:
{'b[gender]': u'male', 'a[gender]': u'female', 'b[name]': u'chris', 'a[name]': u'alexis', 'b[address]': u'boston', 'a[address]': u'boston'}
when want this:
{'b': {'gender' : u'male', 'name': u'chris', 'address': u'boston'}, 'a': {'gender': u'female', 'name': u'alexis', 'address': u'boston'}}
why post body not maintaining hierarchy on server side? error client side encoding logic? on server side flask decoding json? ideas?
thanks
the error client side mapping. mapping needs represent structure of data want , relationships contains. mapping uses keypaths hide structural relationships.
you need 2 mappings:
- the dictionary of parameters
- the container of dictionaries
the mappings defined as:
parammapping = [rkobjectmapping requestmapping]; containermapping = [rkobjectmapping requestmapping]; [parammapping addattributemappingsfromarray:@[ @"name", @"address", @"gender", ]]; rkrelationshipmapping *arelationship = [rkrelationshipmapping relationshipmappingfromkeypath:@"a" tokeypath:@"a" withmapping:parammapping]; rkrelationshipmapping *brelationship = [rkrelationshipmapping relationshipmappingfromkeypath:@"b" tokeypath:@"b" withmapping:parammapping] [containermapping addpropertymapping:arelationship]; [containermapping addpropertymapping:brelationship];
then request descriptor defined using container mapping.
Comments
Post a Comment