NSURLSession request and response
Asked Answered
C

1

12

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

Colwell answered 13/10, 2016 at 8:50 Comment(4)
there are dozens of available answers of this questionBlate
raywenderlich.com/110458/nsurlsession-tutorial-getting-startedCq
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.Colwell
On Google, "NSURLSession GEt site:stackoverflow.com Objective-C" search gives #7673627 Did you do any research/tries on your side ?Marlite
C
29

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];
Christianly answered 13/10, 2016 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.