我想使用 AFNetworking 调用“freshdesk”的 API。并且该 API 在其他客户端上运行良好。但我想使用 AFNetworking 从 iOS 应用程序端调用该 API 但我收到以下错误:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fd4a1579ed0> { URL: https://leotest.freshdesk.com/api/v2/tickets } { status code: 415, headers {
Connection = "keep-alive";
"Content-Length" = 145;
"Content-Type" = "application/json";
Date = "Fri, 09 Dec 2016 04:45:22 GMT";
Status = "415 Unsupported Media Type";
"X-Rack-Cache" = "invalidate, pass";
"X-RateLimit-Remaining" = 4966;
"X-RateLimit-Total" = 5000;
"X-RateLimit-Used-CurrentRequest" = 1;
"X-Request-Id" = e3f6a1bcd872f476d1701ce0dd7b5f54;
} }, NSErrorFailingURLKey=https://leotest.freshdesk.com/api/v2/tickets, com.alamofire.serialization.response.error.data=<7b22636f 6465223a 22696e76 616c6964 5f636f6e 74656e74 5f747970 65222c22 6d657373 61676522 3a22436f 6e74656e 742d5479 70652068 65616465 72206973 20736574 20746f20 6170706c 69636174 696f6e2f 782d7777 772d666f 726d2d75 726c656e 636f6465 642e2049 74207368 6f756c64 20626520 73657420 746f2061 70706c69 63617469 6f6e2f6a 736f6e22 7d>, NSLocalizedDescription=Request failed: unsupported media type (415)}
我认为问题是当我在 API 中传递数据时
NSDictionary *params=@{@"description":@"This is testing issue",
@"subject":@"Support needed..",
@"email":@"abc@gmail.com",
@"priority":[NSNumber numberWithInt:1],
@"status":[NSNumber numberWithInt:2]};
[webServiceController PostWithHeaderUrlCall:@"https://test.freshdesk.com/api/v2/tickets" Param:params];
网络服务控制器.m
-(void)PostWithHeaderUrlCall:(NSString *)IN_URL Param:(NSDictionary*) IN_Param
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
NSData *basicAuthCredentials = [[NSString stringWithFormat:@"%@:%@",@"fsdwt52ewr5325wer5",@"x"] dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64AuthCredentials = [basicAuthCredentials base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0];
[requestSerializer setValue:[NSString stringWithFormat:@"Basic %@", base64AuthCredentials] forHTTPHeaderField:@"Authorization"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/json"];
manager.requestSerializer = requestSerializer;
[manager POST:IN_URL parameters:IN_Param progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
m_Status=1;
self.dic_Response = (NSDictionary *)responseObject;
[[NSNotificationCenter defaultCenter] postNotificationName:m_strPostMsg object:self];
} failure:^(NSURLSessionTask *operation, NSError *error) {
m_Status=0;
self.dic_Response=[[NSMutableDictionary alloc]init];
[self.dic_Response setValue:Time_Out_Msg forKey:@"message"];
NSLog(@"%@",error);
[[NSNotificationCenter defaultCenter] postNotificationName:m_strPostMsg object:self];
}];
}