I wanna make a POST request using RestKit, I work with RKObjectManager using a method postObject. The body of the request is an array of integers, JSON representation is something like [2348, 9864, 3645]. I have added a RKResponseMapping for response of service and work fine, but on service side always an array is null. What is a RKRequestMapping then I have to add? What I missing?
My code look like:
- (void) sendPOSTRequest:(NSArray*) postBody{
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:baseUrl]];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[RKJSONArrayResponse class]];
[responseMapping addAttributeMappingsFromDictionary:@{ @"Code" : @"code",
@"Success":@"success",
@"Content":@"content",
@"Error": @"error"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
method:RKRequestMethodPOST
pathPattern:@"/api/Requisitions/SendReport"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[NSArray class]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[NSArray class]
rootKeyPath:nil,
method:RKRequestMethodPOST];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
[self.manager postObject:postBody
path:@"/api/Requisitions/SendReport"
parameters:nil
success:success
failure:failure];
}
The service code is written in C # with Web Api, here is where you will always receive the variable "report" as null.:
public JsonResponse<int[]> SendReport(int[] report){}
You have 2 ways to solve this problem:
if you want to get it done right, you need to create RKObjectMapping
for an array of your integer values, then you need to create RKRequestDescriptor
that will use your newly created RKObjectMapping
and then you'll add this RKRequestDescriptor
to your RKObjectManager
using method – addRequestDescriptor:
. This will make RestKit to transform an object you're passing to postObject
method into JSON according to your RKObjectMapping
I do not recommend this way. But if you just want to play around and see what you can do with minimal effort, you should pass an array of integers as parameters
argument, like this:
NSArray *body = @[ @1234, @7475, @4432 ];
[manager postObject:nil path:@"/send" parameters:body ...]
Beware, that parameters argument is NSDictionary
, but if you pass NSArray
it'll work and will trigger a warning.
Again, spend time to get familiar with RKRequestDescriptor
and RKObjectMapping
.
If you want more information on best RestKit setup and how to build maintainable app with few ObjectManagers.