Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restkit 0.2 postObject always returns request.body=(null)

Tags:

ios

restkit

I am banging my head with restkit 0.2 and posting objects. I am trying to post an object with mapped requestDescriptor but always get a request.body=(null) in my request. I am currently totally stuck.

I am using the following code for mapping

// Construct a request mapping for our class
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{
 @"command": @"requestedCommand" ,
 @"payload": @"payloadForCommand"
 }];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[ShoppingListAPI class] rootKeyPath:nil];
[manager addRequestDescriptor:requestDescriptor];

// create the object
ShoppingListAPI *shoppingListRequest = [[ShoppingListAPI alloc] init];
shoppingListRequest.payload = payload;
shoppingListRequest.command = @"getShoppingLists";

and try to post it with

[manager postObject:shoppingListRequest
                                path:@"shoppinglistWebservice.html"
                                parameters:nil
                                 success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
                                            DLog(@"We object mapped the response with the following result: %@", result);
                                    }
                                failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                        DLog(@"Hit error: %@", error);

                                }];

From the logs I get the following

T restkit.network:RKHTTPRequestOperation.m:150 POST 'http://www.myhost.com/shoppinglistWebservice.html': 
request.headers={
Accept = "application/json";
"Accept-Language" = "de, en, fr, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8";
"Content-Type" = "text/html; charset=utf-8";
"User-Agent" = "myapp/1.0 (iPhone Simulator; iOS 6.0; Scale/2.00)";
}
request.body=(null)

I followed the example implementation from RKObjectManager.h with no success. request.body stays null. Isn't it supposed to show my object as JSON string? Mapping is working fine, i.e.

Mapped attribute value from keyPath 'command' to 'requestedCommand'. Value: getShoppingLists

Any ideas?

like image 865
Martin W. Avatar asked Aug 15 '26 15:08

Martin W.


2 Answers

RKRequestDescriptor *reqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:objectClass rootKeyPath:path];
[objectManager addRequestDescriptor:reqDescriptor];

For mapping use [mapping inverseMapping]

For objectClass [Someclass class]. Path can be nil mostly.

like image 66
Leszek Zarna Avatar answered Aug 18 '26 07:08

Leszek Zarna


I don't know if you managed to solve this, but I finally worked out why I was unable to post - it was very frustrating:

I needed to add this:

objectManager.requestSerializationMIMEType=RKMIMETypeJSON; 

to get the request to post as JSON.

Problem solved from that point on. Can't work out why this isn't a more widespread answer

like image 43
rob123 Avatar answered Aug 18 '26 06:08

rob123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!