2016-12-09 43 views
0

我要調用的API‘的Freshdesk’使用AFNetworking這API從靜止正常使用client.But我想打電話從iOS應用,API。側面採用AFNetworking但我得到以下錯誤:響應代碼= -1011「請求失敗:不支持的媒體類型(415)

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 *[email protected]{@"description":@"This is testing issue", 
           @"subject":@"Support needed..", 
           @"email":@"[email protected]", 
           @"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]; 

    }]; 

} 
+0

你在Web服務通過哪些類型的媒體? –

+0

@HimanshuMoradiya我通過Nsdictionary的PNMS沒有像MP3文件的圖像 –

+0

我想問題是,我必須通過在API參數中的JSON –

回答

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

我想你的代碼,但我得到了同樣的錯誤 –

+0

@YogendraPatel嘗試新更新的代碼只是你的一個替換並檢查它。 –

+0

好的,讓我試試。@ Himanshu Moradiya –

相關問題