Robert J. Clegg
Robert J. Clegg

Reputation: 7360

How to use AFHTTPSessionManager for a GET request?

So I want to use AFNetworking V2.0 for a GET request with NSURLSession (iOS7's new API)

So far I have got this - but is this the correct way to do it?

NSString *tempURL =[NSString stringWithString:url];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager GET:tempURL parameters:params success:^(NSURLSessionDataTask *task, id responseObject)
     {
         //Working

     }failure:^(NSURLSessionDataTask *task, NSError *error)
     {
         //Failed!!

     }];

Would this be the correct way to do it?

Upvotes: 1

Views: 12680

Answers (1)

tolgamorf
tolgamorf

Reputation: 809

I think what you are missing is an alloc-init for the AFHTTPSessionManager.

Something like this should work (untested):

NSURL *baseURL = [NSURL URLWithString:@"http://example.com/api"];
NSString *path = @"resource/1";

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];

[manager GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject)
 {
     // Success
     NSLog(@"Success: %@", responseObject);
 }failure:^(NSURLSessionDataTask *task, NSError *error)
 {
     // Failure
     NSLog(@"Failure: %@", error);
 }];

This would send a GET request to http://example.com/api/resource/1.

Upvotes: 5

Related Questions