AFnetworking保持返回响应代码= -1011"请求失败:客户端错误(422)

时间:2014-10-20 15:12:46

标签: ios objective-c http request afnetworking

我尝试使用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 {

}

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

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

1 个答案:

答案 0 :(得分: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);


    }];

希望这会有所帮助。