Reputation: 209
I'm try to use AFNetworking to create a POST request. However I always return an error says:
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)}
Here is my code:
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 {
}
I'm petty sure the parameters I provided (subject, description, email)is corrent, becuase if I put the parameters directly in the url like below, it works fine.
https://isisfriends.zendesk.com/requests/mobile_api/create.json?subject=testing&[email protected]&description=testing
Upvotes: 0
Views: 3899
Reputation: 158
422 client error is for failed authentication. Check if you need any authentication or if you think You have permission, Look for the logged Failure error serialised
in the console to check what server has to say about your call.
Use this simplified code:
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);
}];
Hope this helps. 😊👍
Upvotes: 1