Satheeshkumar Naidu Satheeshkumar Naidu - 1 year ago 41
Objective-C Question

How to call web service using post method with request paramters for the below api

I have an path:


By using this path i want to post my video using the below API:<image_path>&image_name=<image_id>

Please help me out .

Answer Source

Sending POST requests in iOS is quite easy; and there's no need for an additional framework.

POST Request:

We begin by creating our POST's body (ergo. what we'd like to send) as an NSString, and converting it to NSData.

NSString *post = [NSString stringWithFormat:@"test=Message&this=isNotReal"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

Next up, we read the postData's length, so we can pass it along in the request.

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

Now that we have what we'd like to post, we can create an NSMutableURLRequest, and include our postData.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@""]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

And finally, we can send our request, and read the reply by creating a new NSURLSession:

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"requestReply: %@", requestReply);
}] resume];