0

I am new to ios development world , i need to send data(one parameter in the body of the request) to server by http post request , how can i do that . (need simple example) . I am using objective c Thanks in advance.

Mohammed Aboelwafa
  • 1,035
  • 1
  • 9
  • 13

1 Answers1

1

Quick and dirty..

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://httpbin.org/post"]];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [@"param=value" dataUsingEncoding:NSUTF8StringEncoding];
    request.allHTTPHeaderFields = @{@"Content-Type": @"application/x-www-form-urlencoded"};

    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    }] resume];

Result:

2016-07-17 12:53:51.250 Snippets[24943:1720590] {
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "param": "value"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Accept-Language": "en-us", 
    "Content-Length": "11", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Snippets/1 CFNetwork/758.3.15 Darwin/15.5.0"
  }, 
  "json": null, 
  "origin": "64.231.255.187", 
  "url": "http://httpbin.org/post"
}
Brandon
  • 22,723
  • 11
  • 93
  • 186