Sending POST and GET requests in iOS is pretty simple; and there is no need for an additional structure.
POST Request:
Let's start by creating a POST body (the ergo that we want to send) as an NSString and converting it to NSData .
objective-c
NSString *post = [NSString stringWithFormat:@"test=Message&this=isNotReal"]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
Next, we read postData length , so we can pass it in the request.
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
Now that we have what we would like to publish, we can create an NSMutableURLRequest and include our postData .
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody:postData];
swift
let post = "test=Message&this=isNotReal" let postData = post.data(using: String.Encoding.ascii, allowLossyConversion: true) let postLength = String(postData!.count) var request = URLRequest(url: URL(string: "http://YourURL.com/FakeURL/PARAMETERS")!) request.httpMethod = "POST" request.addValue(postLength, forHTTPHeaderField: "Content-Length") request.httpBody = postData;
And finally, we can send our request and read the response by creating a new NSURLSession :
objective-c
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(@"Request reply: %@", requestReply); }] resume];
swift
let session = URLSession(configuration: .default) session.dataTask(with: request) {data, response, error in let requestReply = NSString(data: data!, encoding: String.Encoding.ascii.rawValue) print("Request reply: \(requestReply!)") }.resume()
GET Request:
With a GET request, this is basically the same, only without HTTPBody and Content-Length .
objective-c
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL/PARAMETERS"]]; [request setHTTPMethod:@"GET"]; 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(@"Request reply: %@", requestReply); }] resume];
swift
var request = URLRequest(url: URL(string: "http://YourURL.com/FakeURL/PARAMETERS")!) request.httpMethod = "GET" let session = URLSession(configuration: .default) session.dataTask(with: request) {data, response, error in let requestReply = NSString(data: data!, encoding: String.Encoding.ascii.rawValue) print("Request reply: \(requestReply!)") }.resume()
On a side note, you can add a Content-Type (and other data) by adding the following to our NSMutableURLRequest . This may be required by the server upon request, for example json .
objective-c
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
The response code can also be read using [(NSHTTPURLResponse*)response statusCode] .
swift
request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept")
Update: sendSynchronousRequest is deprecated from ios9 and osx-elcapitan (10.11) and out.
NSURLResponse *requestResponse; NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil]; NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding]; NSLog(@"requestReply: %@", requestReply);