Martin W.
Martin W.

Reputation: 41

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

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?

Upvotes: 3

Views: 1786

Answers (2)

rob123
rob123

Reputation: 493

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

Upvotes: 1

Leszek Zarna
Leszek Zarna

Reputation: 3317

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.

Upvotes: 1

Related Questions