AFNetworking请求失败:不可接受的内容类型:text / plain,

时间:2016-05-29 20:53:45

标签: ios objective-c iphone serialization afnetworking

我一路尝试,但收到错误

  

NSLocalizedDescription = 请求失败:错误请求(400),   com.alamofire.serialization.response.error.data =< 48545450 20343030   0a4d6973 73696e67 20706172 616d6574 65723a20 27726566 6572656e   636527>,NSUnderlyingError = 0x7a81aa80 {错误   Domain = com.alamofire.error.serialization.response Code = -1016“请求   失败:不可接受的内容类型:text / plain“   UserInfo = {com.alamofire.serialization.response.error.response = {URL:https:/// mobile / v1 / document / get} {   状态代码:400,headers {       “Cache-Control”=“no-cache,private”;       Connection =“keep-alive”;       “Content-Type”=“text / plain; charset = UTF-8”;       Date =“Sun,2016年5月29日20:31:44 GMT”;       Server =“nginx / 1.4.6(Ubuntu)”;       “Strict-Transport-Security”=“max-age = 2592000; includeSubDomains”;       “转移编码”=身份;       “X-Exception-Class”=“PM \ PFTAPIBundle \ Exception \ MissingAPIParameterException”;       “X-Exception-Message”=“缺少参数:'参考'”;       “X-Frame-Options”= SAMEORIGIN;       “X-Processing-Time”=“906.5ms”; ,NSErrorFailingURLKey = https:/// api / mobile / v1 / document / get,   NSLocalizedDescription =请求失败:不可接受的内容类型:   text / plain,com.alamofire.serialization.response.error.data =< 48545450   20343030 0a4d6973 73696e67 20706172 616d6574 65723a20 27726566   6572656e 636527>}}}请求失败:错误请求(400)

- (void)postResource:(NSString*)resource queryString:(NSDictionary*)queryString withCompletionHander:(CompletionHandlerBlock)completionHandler
{

NSString *qs = [DictionaryUtils convertDictToJSONString:queryString];

//    self.sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
//    self.sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];//[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

//    [self.sessionManager.requestSerializer setValue:[NSString stringWithFormat:@"PLSID %@",sessionID] forHTTPHeaderField:@"Authorization"];
resource = [[self.sessionManager.baseURL URLByAppendingPathComponent:@"document/get"] absoluteString];

NSURL *base = [NSURL URLWithString:resource];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:base
                                                        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                    timeoutInterval:60.0];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:mACCEPTVALUE forHTTPHeaderField:mCONTENTTYPELABEL];
if ([[[UserDetails sharedInstance]getUserDetails]objectForKey:mSESSIONIDKEY]) {
    NSString *headerField = [NSString stringWithFormat:@"%@ %@",mSOURCEPLSID,sessionID];
    [theRequest setValue:headerField forHTTPHeaderField:mSOURCEIDENTIFIERHEADER];
}
if(qs != nil) {
    [theRequest setHTTPBody:[qs dataUsingEncoding:NSUTF8StringEncoding]];
}


NSURLSessionDataTask *dataTask = [self.sessionManager dataTaskWithRequest:theRequest completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    //
    NSLog(@"responseObject %@", responseObject);
    NSLog(@"%@ \n%@",error, [error localizedDescription]); //{Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4}}}
}];

[dataTask resume];

}

当我使用邮递员测试 api 并将Body中的参数传递为 原始 数据{"reference":{"Id":"User","documentId":"56e0"}} [ 注意:不在 x-www-form-urlencoded或form-data]中。 It works and getting response with data.

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您使用的是AFNetworking,请尝试明确接受内容类型

let serviceManager = AFHTTPSessionManager()
serviceManager.responseSerializer.acceptableContentTypes = NSSet(objects: "text/plain", "text/html", "application/json", "audio/wav", "application/octest-stream", "text/javascript") as? Set

这对我有用