Search code examples
iosobjective-cafnetworking

"Data is Nil" value from delete the api values in ios


I need to delete some items in the api. I used the AFNetworking 2.3.4 version for api request, response. Here i used the DELETE method for delete the api items, when i call this api it delete the items successfully but it throws the error "Data is nil". Why it throws this error i don't know exactly.

Here is my Code for API Delete Method:

// Delete API with Access Token using Arraylist

           +(void)DeleteDataFromApiWithAccessToken:(NSString *)url withParameters:(NSDictionary *)parameters accessTokenValue:(NSString *)tokenValue returnBlock:(arrayList)block
        {
               NSLog(@"loadDataFromApi withParameters :: url %@, params %@",url,parameters);


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];

[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

[serializer setValue:tokenValue forHTTPHeaderField:ACCESSTOKEN_HEADER_FIELD];
[serializer setValue:FF_API_HEADER_VALUE forHTTPHeaderField:FF_API_HEADER_FIELD];

//serializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD", nil];

manager.requestSerializer.timeoutInterval = 0.7;
manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

manager.requestSerializer = serializer;

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

// Check the Network Connection
if(![CommonClassApp checkForNetwork]) {
    [CommonClassApp customAlert:ERR_MSG_NETWORK_PROBLEM];
    if (block) block(NULL);
}else {
    [manager DELETE:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSLog(@"RESPONSE STATUS CODE: %ld", [responseObject statusCode]);
        if (responseObject != NULL)
        {
            NSError *error = nil;

            NSMutableArray *responseJsonData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];

            NSLog(@"responseJsonData - %@",responseJsonData);
            if (block) block(responseJsonData);
        }else{
            [CommonClassApp customAlert:ERR_MSG_INVALID_RESPONSE];
            if (block) block(NULL);
        }

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


        NSLog(@"failure ::: %ld",(long)[operation.response statusCode]);
        NSDictionary *responseJsonData = [NSJSONSerialization JSONObjectWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] options:NSJSONReadingAllowFragments error:&error];
        if ([operation.response statusCode] == 401) {
            if (responseJsonData.count>0) {
                NSString *userError = [responseJsonData valueForKey:@"code"];
                [CommonClassApp customAlert:[ERROR_AUTH_ERROR valueForKey:userError]];
            }
        }
        else
        {
            NSString *userError = [responseJsonData valueForKey:@"message"];
            NSLog(@"Error Message: %@",userError);
            [CommonClassApp customAlert:userError];
        }

        if (block) block(NULL);
    }];

Solution

  • There's nothing wrong with the API, what's wrong is the expectations of your code.

    You asked the API to delete a resource. So what kind of response to you expect? Status 200 and some data? Of course not. You just deleted the resource. It's not there.

    See this answer: HTTP status code for update and delete?

    Summary: You should expect and accept status 200 with data describing that the delete operation succeeded, or status 204 with no data whatsoever, as having successfully deleted the data. (Some people would also accept 202 which basically means the server is saying "I heard you, I'm going to delete it").