响应代码= -1011"请求失败:不支持的媒体类型(415)

时间:2016-12-09 04:53:59

标签: ios objective-c afnetworking freshdesk

我想调用" freshdesk"的API。使用AFNetworking。并且该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];

WebServiceController.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];

     }];

}

1 个答案:

答案 0 :(得分:0)

AFNetworking 3.0使用此代码在服务器上传递数据。

 NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
 [params setValue:YOURV_VALUE forKey:@"description"];
 [params setValue:YOURV_VALUE forKey:@"subject"];
 [params setValue:YOURV_VALUE forKey:@"email"];
  [params setValue:YOURV_VALUE forKey:@"priority"];
[params setValue:YOURV_VALUE forKey:@"status"];

[webServiceController PostWithHeaderUrlCall:@"https://test.freshdesk.com/api/v2/tickets" Param:params];

-(void)PostWithHeaderUrlCall:(NSString *)IN_URL Param:(NSMutableDictionary*) _params
{
        AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:YOUR_URL]];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    [manager POST:url parameters:_params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"response = %@", responseObject);
        if( _success )
        {
            _success( responseObject ) ;
        }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"error = %@", error);
            if( _failure )
            {
                _failure( error) ;
            }
        }];
    }
    else{
        [Utility showAlertWithMessage:@"No Internet Connection"];
    }
   }
 }