AFnetworking 不断返回响应 Code=-1011 “请求失败:客户端错误 (422)

Posted

技术标签:

【中文标题】AFnetworking 不断返回响应 Code=-1011 “请求失败:客户端错误 (422)【英文标题】:AFnetworking keep returning response Code=-1011 "Request failed: client error (422) 【发布时间】:2014-10-20 15:12:46 【问题描述】:

我正在尝试使用 AFNetworking 创建一个 POST 请求。但是我总是返回一个错误说:

    Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: client error (422)" UserInfo=0x7ff1fa76a5c0 com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7ff1fa76ce40>  URL: https://isisfriends.zendesk.com/requests/mobile_api/create.json   status code: 422, headers 
    "Cache-Control" = "no-cache";
    Connection = "keep-alive";
    "Content-Length" = 33;
    "Content-Type" = "application/json; charset=UTF-8";
    Date = "Mon, 20 Oct 2014 14:56:54 GMT";
    P3P = "CP=\"NOI DSP COR NID ADMa OPTa OUR NOR\"";
    Server = nginx;
    "Set-Cookie" = "_zendesk_shared_session=eyJpZCI6IjUxYjdmOGFjMzZjMzE1MjRjNDE0OTFiMjRmYmYzNjhhIiwibG9jYWxlX2lkIjoxfQ%3D%3D--229c90ddd7cf33dc5886aba445fd51cccaf69ea7; path=/; secure; HttpOnly, _zendesk_session=BAh7CkkiD3Nlc3Npb25faWQGOgZFVEkiJWVhZTRmZTQ5ZDQ1NmZjOTgzZDBlMzgyMWQ5YjMwMjNlBjsAVEkiDGFjY291bnQGOwBGaQMdNgdJIgpyb3V0ZQY7AEZpAuq9SSIOaXNfbW9iaWxlBjsAVFRJIhN3YXJkZW4ubWVzc2FnZQY7AFR7AA%3D%3D--c8bf5a2774eb5fdaa8ff7ec2da6adef3f76b15c3; path=/; secure; HttpOnly";
    Status = "422 Unprocessable Entity";
    Vary = Accept;
    "X-Frame-Options" = SAMEORIGIN;
    "X-Rack-Cache" = "invalidate, pass";
    "X-Request-Id" = b71fc58dc1cf122d395b77968aff9014;
    "X-Runtime" = "0.088859";
    "X-UA-Compatible" = "IE=Edge,chrome=1";
    "X-XSS-Protection" = "1; mode=block";
    "X-Zendesk-Origin-Server" = "app13.pod2.sac1.zdsys.com";
    "X-Zendesk-Request-Id" = 10c9143fd5ac87ab66d3;
 , NSErrorFailingURLKey=https://isisfriends.zendesk.com/requests/mobile_api/create.json, com.alamofire.serialization.response.error.data=<7b226572 726f7222 3a22496e 76616c69 6420656d 61696c20 61646472 65737322 7d>, NSLocalizedDescription=Request failed: client error (422)

这是我的代码:

NSString * question = [_textFieldQuestion text];
NSString * detail = [_textFieldDetails text];
NSString * email = [_textFieldEmail text];

if(question.length > 0 && detail.length > 0 && email.length > 0)

    NSString *url = @"https://isisfriends.zendesk.com/requests/mobile_api/create.json";
    NSDictionary *parameters = @@"subject":question, @"description":detail, @"email":email;


    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];


    AFJSONRequestSerializer *reqSerializer = [AFJSONRequestSerializer serializer];
    [reqSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [reqSerializer setValue:@"1.0" forHTTPHeaderField:@"X-Zendesk-Mobile-API"];


    manager.requestSerializer = reqSerializer;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    NSLog(parameters.descriptionInStringsFileFormat);

    [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
        NSLog(@"===== JSON: ======= %@", responseObject);
     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
        NSLog(@"============== Error: ============\n%@", [error description]);
    ];


 else 


我很确定我提供的参数(主题、描述、电子邮件)是正确的,因为如果我将参数直接放在下面的 url 中,它可以正常工作。

https://isisfriends.zendesk.com/requests/mobile_api/create.json?subject=testing&email=testing@asd.com&description=testing

【问题讨论】:

您尝试过使用表单请求link 吗? 【参考方案1】:

422 客户端错误是因为身份验证失败。检查您是否需要任何身份验证,或者您是否认为您有权限,在控制台中查找已记录的 Failure error serialised 以检查服务器对您的呼叫有什么看法。

使用这个简化的代码:

    NSString *myUrlString= YOUR LINK;

    NSMutableDictionary* postRequestDictionary = [[NSMutableDictionary alloc] init];

    postRequestDictionary[@"YOUR PARAMATER"]= YOUR PARAMETER VALUE;

 //  ... ADD ANY MORE PARAMETER IF YOU WANT HERE ...

    NSLog(@"body = %@",postRequestDictionary);

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"HEADER VALUE" forHTTPHeaderField:@"HEADER"];

 //   ... ADD ANYMORE HEADER IF YOU WANT ...


    [manager POST:myUrlString parameters:postRequestDictionary success:^(AFHTTPRequestOperation *requestOperation,id JSON)

        NSLog(@"%@",JSON);


     failure:^(AFHTTPRequestOperation *requestFailureOperation , NSError *error)


        NSLog(@"%@",error);
        NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];

        NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil];
        NSLog(@"Failure error serialised - %@",serializedData);


    ];

希望这会有所帮助。 ??

【讨论】:

以上是关于AFnetworking 不断返回响应 Code=-1011 “请求失败:客户端错误 (422)的主要内容,如果未能解决你的问题,请参考以下文章

强制将响应对象返回给 AFNetworking

AFNetworking 在 NSError 对象中返回响应

AFNetworking 2.0:POST 返回成功响应但服务器未更新

AFNetworking:无法从 AFHTTPRequestOperation 获取响应字符串

为啥这个 API 在 AFNetworking 中将其响应作为“失败”返回,而它正是它应该是的并且状态码是 200?

在 AFNetworking 2 的失败块中访问 JSON 响应