AFNetworking 2.0 Get JSON from code 400 in failure block

前端 未结 4 2014
长发绾君心
长发绾君心 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 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
    

提交回复
热议问题