问题
My implementation is not sending parameters. Here is the RequestDescriptor:
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addAttributeMappingsFromArray:@[@"nombres", @"telefono", @"direccion", @"email", @"identificacion", @"tipo_id", @"recibir_sms_ordenes", @"recibir_correos_ordenes"]];
RKRequestDescriptor *descriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[RKUser class] rootKeyPath:@"me" method:RKRequestMethodAny];
[[RKObjectManager sharedManager] addRequestDescriptor:descriptor];
Here is the post request:
RKUser *user = [self currentUser].user; // [self currentUser].user returns the current RKUser
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager postObject:user path:@"/api/me.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@ %@", operation, mappingResult);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"%@ %@", error, operation);
}];
In the server, I only receive one empty current_user
parameter. Any ideas on why?
PD. The server expects JSON payload under me
key
回答1:
Ensure that you have set:
manager.requestSerializationMIMEType = RKMIMETypeJSON;
As the default is RKMIMETypeFormURLEncoded
.
Based on your comments and discussion, the mapping isn't being used because the class of the user is wrong. This is because it is a plain NSManagedObject
rather than an RKUser
instance. This means RestKit can't find the appropriate mapping. The cause for this is the custom class not being set on the User
entity in your Core Data model.
来源:https://stackoverflow.com/questions/20509963/restkit-request-not-sending-parameters