11

To understand how GET requests are made using NSURLSession in Objective-C, I would like an example. And, how is a response obtained?

h.and.h
  • 690
  • 1
  • 8
  • 26
FreshStar
  • 193
  • 1
  • 1
  • 10
  • there are dozens of available answers of this question – Ahmad F Oct 13 '16 at 08:51
  • https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started – Krutarth Patel Oct 13 '16 at 08:54
  • 1
    Sir,I am using objective c.Please suggest some tutorial that explains how "GET" requests are made and response is obtained from server using NSURLSession in objective c. – FreshStar Oct 13 '16 at 09:04
  • 5
    On Google, "NSURLSession GEt site:stackoverflow.com Objective-C" search gives http://stackoverflow.com/questions/7673127/how-to-send-post-and-get-request Did you do any research/tries on your side ? – Larme Oct 13 '16 at 09:10

1 Answers1

27

GET

NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"give your url here"]];

//create the Method "GET" 
[urlRequest setHTTPMethod:@"GET"];

NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  if(httpResponse.statusCode == 200)
  {
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"The response is - %@",responseDictionary);
  }
  else
  {
    NSLog(@"Error");     
  }
}];
[dataTask resume];
user3182143
  • 9,459
  • 3
  • 32
  • 39