Oleg Oleg - 1 year ago 167
iOS Question

How to do GET request via AFNetworking?

I added directories

to project manager.
I use latest library 3.0.4

After I imported file
in my class file
I found a lot examples how to make request to url, but information is old.

How to make GET request to URL and get data from server?
Now function
was removed.

This is first time working with libraries in xcode, I am beginner.

Answer Source
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://google.com/"]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // Print the response body in text
    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
[operation start];


You can also use AFHTTPRequestOperation:

NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];

networkQueue.maxConcurrentOperationCount = 5;

NSURL *url = [NSURL URLWithString:@"https://example.com"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

    NSLog(@"%@", string);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
[networkQueue addOperation:operation];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download