greenLizard
greenLizard

Reputation: 2346

Send post request in XML format using RestKit

Hi I am using restkit for first time, and there are several questions that come to my mind. First when sending a post request using restkit what format is the request Json or XML and how can I specify it? I am sending a post request to the server to authenticate a user and should receive a conformation if details correct in XML format.

NSArray *objects = [NSArray arrayWithObjects: email, password, nil];
NSArray *keys = [NSArray arrayWithObjects:@"username", @"password", nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

[[RKClient sharedClient] post:@"/login" params:params delegate:self]; 

This is the code I am using, the xml accepted by the web services should look like

<login>
  <username>[email protected]</username>
  <password>Password</password>
</login>

It is sending the request,but I am not getting the right response. Is there a way to view what is the format of the request I am sending to the server ?

Upvotes: 1

Views: 1804

Answers (5)

Imran Raheem
Imran Raheem

Reputation: 880

You can use RKRequestSerialization class to do the xml serialization for you.

Here is a code snippet from one of my projects:

#import <RestKit/RKRequestSerialization.h>

...

[RKObjectManager sharedManager].acceptMIMEType = RKMIMETypeTextXML;

NSString *loginData = @"<Login><UserId>test</UserId><Password>test</Password></Login>";

[[RKClient sharedClient] setValue:@"XYZ01" forHTTPHeaderField:@"ServiceId"]; 
[[RKClient sharedClient] post:@"/login" 
                       params:[RKRequestSerialization serializationWithData:[loginDetails dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeTextXML] 
                     delegate:self];

And, here is the dump using RKLogConfigureByName("RestKit/Network", RKLogLevelTrace)

2012-08-12 11:28:41.476 MyApp[730:707] T restkit.network:RKRequest.m:402 Prepared POST URLRequest '<NSMutableURLRequest https://someserver.com/MyApp/rest/service/request>'. HTTP Headers: {
    Accept = "text/xml";
    "Content-Length" = 75;
    "Content-Type" = "text/xml";
    ServiceId = XYZ01;
}. HTTP Body: <Login><UserId>test</UserId><Password>test</Password></Login>

Upvotes: 2

Carl
Carl

Reputation: 1832

I solved this using ASIHTTPRequest and XMLWriter. Unfortunate, but RestKit just doesn't seem to support XML posts out the box.

I do use RK for GETting stuff though.

Upvotes: 0

Andrei
Andrei

Reputation: 11

I did objectManager.serializationMIMEType = RKMIMETypeXML; but the Content-Length of my request is 0, and the HTTP Body is empty. If I use RKMIMETypeJSON the request looks fine, but the xml serialization doesn't work. Any ideas why this happens? What else do I need to do to post xml with valid serialization?

I also found this in doc: "RestKit currently supports serialization to RKMIMETypeFormURLEncoded and RKMIMETypeJSON". So, how can I use RKObjectManager to post xml?

Upvotes: 1

Paul Cezanne
Paul Cezanne

Reputation: 8741

Give this a try. I think it is supposed to do what you want. I never got it to work but I had other things wrong with my code.

    RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://mysite.com"];

    objectManager.serializationMIMEType = RKMIMETypeXML;

Note: this may be what you do for sending XML back to you, not sending XML to the server. Don't know off the top of my head.

Upvotes: 2

Tom Andersen
Tom Andersen

Reputation: 7200

Sounds like you are using the wrong call. The post call you are using assumes that the rest service wants params like login=username&password=skdjgh, i.e. NOT in XML, but in 'normal REST format'. You need to either find a call to post a block of text using RestKit, or use another call. In other words you need to create the XML yourself (or use some library) then send that via a post.

Upvotes: 2

Related Questions