AFNetworking 2.0 Get JSON from code 400 in a failure block

I am using AFHTTPRequestOperationManager to request a POST . Now I'm intentionally entering the wrong information to handle the 400 error code. Now the web service actually returns JSON with a message explaining to the user what they did wrong. I would really like this JSON display the message in a UIAlertView . However, the failure unit:

 [operationManager POST:ServerURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: Status Code: %d", operation.response.statusCode); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failed: Status Code: %d", operation.response.statusCode); }]; 

does not skip the response object as an object in the success block. So does anyone know how I can access the JSON returned by the web service with a 400 error? NSError *error just gives me Request failed: bad request (400) rather than returning JSON.

Any help would be appreciated, Mike

+6
source share
4 answers

Having looked at the code - responseObject , it seems that the HTTP error does not allow it to be populated. You can directly capture responseData and analyze it yourself, but I would say that this is either a bug or a good improvement request. (It seems that - responseObject should probably check self.responseSerializationError rather than self.error when deciding whether to try to create a response object.)

+8
source

You can make any of these decisions.

1) Set acceptableStatusCodes to accept your 400 statusCode and you process the success block

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

2) Create your own ResponseSerializer, for example, JSONResponseSerializerWithData , to insert a responseObject into the NSError user information and process it in the fault block

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

 setCompletionBlockWithSuccess:failure: responseObject error 
+4
source

The following code is 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
source

I also encountered the same problem in AFNetworking as instead of

 - (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 

Try to use it: -

 - (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
source

Source: https://habr.com/ru/post/956786/


All Articles