【问题标题】:Response Code=-1011 "Request failed: unsupported media type (415)响应代码=-1011“请求失败:不支持的媒体类型 (415)
【发布时间】:2016-12-09 04:53:59
【问题描述】:

我想使用 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];

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

     }];

}

【问题讨论】:

  • 您在网络服务中传递哪种类型的媒体?
  • @HimanshuMoradiya 我在 parms 中通过了 Nsdictionary,与 mp3 文件的图像完全不同
  • 我认为问题是,我必须在 API 参数中传递 json
  • 如果你只想传递文本数据,那么你也可以使用 NSMutableDictionary 而不是 Dictionary 并且它工作正常。对我来说。
  • 但是这个API接受Json数据格式@HimanshuMoradiya

标签: ios objective-c afnetworking freshdesk


【解决方案1】:

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"];
    }
   }
 }

【讨论】:

  • @YogendraPatel 尝试新的更新代码只需将其替换为您的代码并检查它。
  • 只需复制我的方法并将其替换为您的方法,然后按照我的提示进行操作。在他们传递你的价值时传递你的价值。 @YogendraPatel
  • 您在哪一行收到此错误?你在使用 AFNetworking 3.0
猜你喜欢
  • 1970-01-01
  • 2015-06-17
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
  • 2014-05-10
相关资源
最近更新 更多