Misha
Misha

Reputation: 685

AFNetworking 3.0 status code

I'm using AFNetworking 3.0 and I'm not able to get status code for success or failure.

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:[NSString stringWithFormat:@"%@%@",BASE_URL,url] parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
                    // TODO: show progress somewhere
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    NSLog(@"%@",responseObject);
                    // get status here
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

                    // get status here
                }];

Upvotes: 2

Views: 3369

Answers (3)

matusalem
matusalem

Reputation: 2531

This is working code:

[afHttpSessionManager GET:@"blablabla/bla" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    // success
} failure:^(NSURLSessionTask *operation, NSError *error) {
    // failure
    NSHTTPURLResponse *response = (NSHTTPURLResponse *) [operation response];
    NSInteger statusCode = [response statusCode];
    NSLog(@"RESPONSE CODE: %i", statusCode);
}];

Upvotes: 3

rushisangani
rushisangani

Reputation: 3385

Hope this helps!!!

typedef enum RequestTypes{

GET,
POST,
PUT,
DELETE

}RequestType;

@property (nonatomic, strong) AFHTTPSessionManager *manager;    

self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:urlString]]; // Base URL here, not entire URL
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];


 //Add Headers to request if any.  // header is a dictionary of key value pair.
for (NSString *key in [headers allKeys]) {
    [self.manager.requestSerializer setValue:[headers valueForKey:key] forHTTPHeaderField:key];
}

/* Serialize request by type */ /* Type can be GET, POST etc */

NSString *requestTypeMethod = [self getStringForRequestType:type];

/* enter your URL here excluding base url  */ 

NSMutableURLRequest *request = [self.manager.requestSerializer requestWithMethod:requestTypeMethod URLString:[[NSURL URLWithString:urlString relativeToURL:_manager.baseURL] absoluteString] parameters:params error:nil];

NSURLSessionDataTask *dataTask = [self.manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

 // here you will get actual response, NSURL response as well  and error if any.      

}];

[dataTask resume];


#pragma mark - GET Request type as string
-(NSString *)getStringForRequestType:(RequestType)type {

NSString *requestTypeString;

switch (type) {
    case GET:
        requestTypeString = @"GET";
        break;

    case POST:
        requestTypeString = @"POST";
        break;

    case PUT:
        requestTypeString = @"PUT";
        break;

    case DELETE:
        requestTypeString = @"DELETE";
        break;

    default:
        requestTypeString = @"GET";
        break;
 }

   return requestTypeString;
}

Upvotes: 1

rushisangani
rushisangani

Reputation: 3385

You have to use method of AFURLSessionManager to get NSURLResponse object like this. As AFHTTPSessionManager is subclass of AFURLSessionManager it provides handy methods for network calls.

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

by using AFURLSessionManager call any method like dataTaskWithRequest or downloadTaskWithRequest, you will get NSURLResponse object and from that you can get status code for success or failure.

Upvotes: 0

Related Questions