Can't print the json if response code is 400 i

2019-09-11 09:33发布

问题:

I am using AFNetworking make post request to server.If my response code is 400 then i am not able to print the json response.

This is my code

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"123", @"post_id",
                            arr_friendsTags, @"tagged_user", nil];
    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [requestSerializer setValue:api_key forHTTPHeaderField:@"Authorization"];
    manager.requestSerializer = requestSerializer;
    AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializer];
    responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    manager.responseSerializer = responseSerializer;
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)

     {
         NSLog(@"JSON: %@", responseObject);
         [self activityIndicator:@"hide"];
         NSDictionary* json =responseObject;


     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         [self activityIndicator:@"hide"];
         NSLog(@"Error: %@", error);
     }];'

回答1:

Response code 400 depicts failure. And hence the control passes to failure block, and not success block.

Status Code 400

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

See this link for a list of HTTP codes.



回答2:

I'd think that status 400 will use the "failure" block, not the "success" block. Have a look at error. But it is quite likely that no JSON is returned by the server for errors.