Handle HTTP error with NSURLSession? Handle HTTP error with NSURLSession? ios ios

Handle HTTP error with NSURLSession?

The second parameter of the completionHandler is the NSURLResponse, which when doing a HTTP request, is generally a NSHTTPURLResponse. So, you'd generally do something like:

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:[self postRequestWithURLString:apiEntry parameters:parameters] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {    // handle basic connectivity issues here    if (error) {        NSLog(@"dataTaskWithRequest error: %@", error);        return;    }    // handle HTTP errors here    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {        NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];        if (statusCode != 200) {            NSLog(@"dataTaskWithRequest HTTP status code: %d", statusCode);            return;        }    }    // otherwise, everything is probably fine and you should interpret the `data` contents    NSLog(@"data: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);}];[dataTask resume];

Swift 3:

// handle basic connectivity issues hereguard error == nil else {    print("Error: ", error!)    return}// handle HTTP errors hereif let httpResponse = response as? HTTPURLResponse {    let statusCode = httpResponse.statusCode    if (statusCode != 200) {        print ("dataTaskWithRequest HTTP status code:", statusCode)        return;    } }if let data = data {    // here, everything is probably fine and you should interpret the `data` contents}

you could try something like this. I've created a simple method that will be able to post a data into server and get the server response. You can get the server status code via NSHTTPURLResponse class.Hope will help :)

-(void) POST:(NSURL *) url URLparameters:(NSString *) parameters success:(void (^)(NSURLSessionDataTask *  task, id   responseObject)) successHandler errorHandler:(void (^)(NSURLSessionDataTask *  task, NSError *  error)) errorHandler{     requestBody = [[NSMutableURLRequest alloc]                   initWithURL:url                   cachePolicy: NSURLRequestUseProtocolCachePolicy                   timeoutInterval:60.0];    [requestBody setHTTPMethod:@"POST"];    [requestBody setValue:@"application/json" forHTTPHeaderField:@"Accept"];    [requestBody setHTTPBody:[NSData dataWithBytes:                              [parameters UTF8String]length:strlen([parameters UTF8String])]];    NSURLSession *session = [NSURLSession sessionWithConfiguration: sessionConfiguration delegate: self delegateQueue: [NSOperationQueue mainQueue]];    NSURLSessionDataTask *task = [session dataTaskWithRequest:requestBody completionHandler:                                  ^(NSData *data, NSURLResponse *response, NSError *error) {                                      NSHTTPURLResponse* respHttp = (NSHTTPURLResponse*) response;                                      if (respHttp.statusCode == SUCCESS) {                                          NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];                                          successHandler(task, dictionary);                                          NSLog(@"HTTP_STATUS: success %@", response);                                      }else if (respHttp.statusCode == UNAUTHORIZE) {                                          NSLog(@"HTTP_STATUS: anauthorize");                                      }else if (respHttp.statusCode== BAD_REQUEST) {                                          NSLog(@"HTTP_STATUS: badrequest");                                      }else if (respHttp.statusCode == INTERNAL_SERVER_ERROR) {                                          NSLog(@"HTTP_STATUS: internalerror");                                      }else if (respHttp.statusCode== NOT_FOUND) {                                          NSLog(@"HTTP_STATUS: internalerror");                                      }                                      errorHandler(task, error);                                      return;                                  }];    [task resume];}