Sergey Gavruk
Sergey Gavruk

Reputation: 3558

Restkit cannot serialize request to xml

I am using RestKit 0.20.0 to work with API (xml).

But when I try to make XML POST request and setup mapping, request body is always null

RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://url:4465/paci/v1.0"]];

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];
objectManager.requestSerializationMIMEType = RKMIMETypeXML;
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeXML];

Mapping:

RKObjectMapping *createServerMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[createServerMapping addAttributeMappingsFromDictionary:@{ @"name": @"name",
                                                           @"description": @"description",
                                                           @"ramSize": @"ram-size",
                                                         }];

RKRequestDescriptor *createServerDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:createServerMapping objectClass:[NSMutableDictionary class] rootKeyPath:@"ve" method:RKRequestMethodPOST];

[objectManager addRequestDescriptor:createServerDescriptor];

Object:

IBCreateServer *createServerRequest = [[IBCreateServer alloc] init];
createServerRequest.name = @"web40";
createServerRequest.description = @"testtest";
createServerRequest.ramSize = @"512";

post Object:

[objectManager postObject:createServerRequest path:@"ve" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {} failure:^(RKObjectRequestOperation *operation, NSError *error) {}];

And the result of this request is:

request.headers={
    Accept = "application/xml";
    "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
}
request.body=(null)

Body is always null. What i am doing wrong? This happens only with request, response XML parsing works correctly.

EDIT: if I serialize to json, it works correctly

Upvotes: 1

Views: 320

Answers (1)

Wain
Wain

Reputation: 119031

Your request descriptor is wrong. It should be:

RKRequestDescriptor *createServerDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:createServerMapping objectClass:[IBCreateServer class] rootKeyPath:@"ve" method:RKRequestMethodPOST];

This tells RestKit what your source class is and what mapping it should use when you ask it to post an instance of that class. The mapping tells RestKit what to convert the instance into (a mutable dictionary) so that it is ready to serialise to XML.

Upvotes: 2

Related Questions