AFNetworking 2.0 Get JSON from code 400 in failure block

前端 未结 4 2013
长发绾君心
长发绾君心 2021-01-05 09:33

I\'m using AFHTTPRequestOperationManager for a POST request. Now I\'m deliberately entering incorrect information to handle a 400 erro

相关标签:
4条回答
  • 2021-01-05 09:49

    Looking at the code for - responseObject, it appears that an HTTP error prevents it from being populated. You can grab the responseData directly and parse it yourself, but I would say this is either a bug or a good enhancement request. (It looks like - responseObject probably should be checking self.responseSerializationError, not self.error, when deciding if it should try to build a response object.)

    0 讨论(0)
  • 2021-01-05 09:51

    I also faced same problem in AFNetworking, as instead of using

    - (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
                      progress:(void (^)(NSProgress * _Nonnull))uploadProgress
                       success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
                       failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure
    

    Please try to use that one :-

    - (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
     constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                      progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
    

    thanks,

    0 讨论(0)
  • 2021-01-05 10:07

    You can do either of these solutions

    1) Set the acceptableStatusCodes to accept your 400 statusCode, and you handle in the success block

    manager.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:400];
    

    2) Create a custom ResponseSerializer, like this JSONResponseSerializerWithData, to insert the responseObject into the NSError userInfo, and handle in the failure block

    Pro tip: AFNetworking is opensource, just take a look at AFHTTPRequestOperation for methods

    setCompletionBlockWithSuccess:failure:
    
    responseObject
    
    error
    
    0 讨论(0)
  • 2021-01-05 10:08

    Following code worded for me:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
        [manager.requestSerializer setValue:@"parse-application-id-removed" forHTTPHeaderField:@"X-Parse-Application-Id"];
        [manager.requestSerializer setValue:@"parse-rest-api-key-removed" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        manager.securityPolicy.allowInvalidCertificates = YES;
    
        NSString *URLString = [NSString stringWithFormat:@"%@/%@", BASE_URL,methodName];
    
    
    
        [manager POST:URLString parameters:requestDict success:^(AFHTTPRequestOperation *operation, id responseObject)
        {
            NSLog(@"JSON: %@", responseObject);
    
            [myDelegate StopIndicator];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            [myDelegate StopIndicator];
        }];
    
    0 讨论(0)
提交回复
热议问题