Reputation: 1563
I have a problem regarding xml calling to REST web service, I have to pass following parameters to my web services in following format;
http://XXX.XXX.XXX.XXX/gayanAPI/GayanRESTService.svc/login
So I write a code for this.But its make Some error. (Response Code: 400) My sample code is below;
//prepare request
NSString *urlString = [NSString stringWithFormat:@"http://XXX.XXX.XXX.XXX/gayanAPI/GayanRESTService.svc/login"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
//set headers
NSString *contentType = [NSString stringWithFormat:@"application/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<Login xmlns="">"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<UserId>%@</UserId>",@"200"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<PassWord>%@</PassWord>",@"123"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<ResId>%@</ResId>",@"1000"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<DeviceId>%@</DeviceId>",@"1234"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</Login>"] dataUsingEncoding:NSUTF8StringEncoding]];
//post
[request setHTTPBody:postBody];
//get response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"Response: %@", result);
//here you get the response
}
What is the reason for it. I this I made a mistake when Im creating XML request. Please help me to solve this? can you give me a code for this? I was fed up with this.
Thanks alot.
Upvotes: 2
Views: 3179
Reputation: 1707
In this line,
[postBody appendData:[[NSString stringWithFormat:@"<Login xmlns="">"] dataUsingEncoding:NSUTF8StringEncoding]];
You need to quote double quotes (") with backslash (\) as follows:
[postBody appendData:[[NSString stringWithFormat:@"<Login xmlns=\"\">"] dataUsingEncoding:NSUTF8StringEncoding]];
I'm not sure if it causes the error, though.
Upvotes: 1