I am creating an iPhone client that will be GET and POST for a REST service. The GET part works fine, but I seem to be unable to do anything POST. The server only accepts xml at the moment. I tried to use ASIHTTPRequest, ASIFormDataRequest and do it myself, but nothing works. Can someone help me? I am new to Objective-C programming.
I get a status code: 200 as a response using the following code in didReceiveResponse:
NSHTTPURLResponse * httpResponse;
httpResponse = (NSHTTPURLResponse *) response;
int myStatus = httpResponse.statusCode;
if (myStatus == 400) {
NSLog(@"Status code: 400");
} else if (myStatus == 200) {
NSLog(@"Status code: 200");
} else {
NSLog(@"Other status code");
Three different approaches follow here ...
Code (ASIHTTPRequest):
NSURL *url = [NSURL URLWithString:@"myUrl.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"<my xml>" dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request startSynchronous];
Code (ASIFormDataRequest):
NSURL *url = [NSURL URLWithString:@"someUrl.com"];
ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:url];
[theRequest setPostValue:@"90" forKey:@"key2"];
[theRequest setPostValue:@"150" forKey:@"key3"];
[theRequest startAsynchronous];
Code (third method):
NSData *myPostData = [[NSString stringWithFormat:@"<my xml>"] dataUsingEncoding:NSUTF8StringEncoding];
NSURL *theURL = [NSURL URLWithString:@"someUrl.com"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-type"];
[theRequest setHTTPBody:myPostData];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
How about all this - a (empty) join: style features? Should any of them contain specific code to connect to the job?
Thanks so much for any help!