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.
Asked
Active
Viewed 56 times
0
-
Which content type is expected on the other site? – Amin Negm-Awad Jul 17 '16 at 16:46
-
i think this would help you http://stackoverflow.com/questions/2346893/tutorials-for-using-http-post-and-get-on-the-iphone-in-objective-c – roozbeh mo Jul 17 '16 at 17:27
1 Answers
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