Problems Using POST from iPhone to REST Service

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]];
    // Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startSynchronous];

Code (ASIFormDataRequest):

NSURL *url = [NSURL URLWithString:@"someUrl.com"];
    ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:url];
//       [theRequest setPostValue:@"" forKey:@"key1"];
     [theRequest setPostValue:@"90" forKey:@"key2"];
    [theRequest setPostValue:@"150" forKey:@"key3"];
     // [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
    [theRequest startAsynchronous];

Code (third method):

NSData *myPostData = [[NSString stringWithFormat:@"<my xml>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
//      NSString *xmlPostData = @"<my xml>";
     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 setValue:@"UTF-8" forHTTPHeaderField:@"Charset"];
    [theRequest setHTTPBody:myPostData];// [xmlPostData dataUsingEncoding:NSUTF8StringEncoding]];// 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!

+3
1
NSURL *url = [NSURL URLWithString:@"http://myURL.com"];

ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url];
NSData *myPostData = [[NSString stringWithFormat:@"<My XML Body>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];

[request addRequestHeader:@"Content-Type" value:@"application/xml"];
[request setPostBody:myMutablePostData];
[request setDelegate:self];
[request startSynchronous];
+2

Source: https://habr.com/ru/post/1779223/


All Articles