我需要删除api中的一些项目。我使用AFNetworking 2.3.4版本来响应api请求。在这里我使用DELETE方法删除api项目,当我调用此api时,它成功删除项目,但它会抛出错误“Data is nil”。为什么它抛出这个错误我不确切知道。
以下是我的API删除方法代码:
//使用Arraylist
删除带有访问令牌的API +(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);
}];
答案 0 :(得分:1)
API没有任何问题,您的代码的期望是什么?
您要求API删除资源。那么你期待什么样的回应呢?状态200和一些数据?当然不是。您只是删除了资源。它不在那里。
请参阅此答案:HTTP status code for update and delete?
摘要:您应该期望并接受状态200,其中数据描述删除操作成功,或者状态204没有任何数据,因为已成功删除数据。 (有些人也会接受202,这基本上意味着服务器正在说"我听说过你,我将删除它")。