Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking Request failed: unacceptable content-type: text/plain,

I tried all the way, but getting error

NSLocalizedDescription=Request failed: bad request (400), com.alamofire.serialization.response.error.data=<48545450 20343030 0a4d6973 73696e67 20706172 616d6574 65723a20 27726566 6572656e 636527>, NSUnderlyingError=0x7a81aa80 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo={com.alamofire.serialization.response.error.response= { URL: https:///mobile/v1/document/get } { status code: 400, headers { "Cache-Control" = "no-cache, private"; Connection = "keep-alive"; "Content-Type" = "text/plain; charset=UTF-8"; Date = "Sun, 29 May 2016 20:31:44 GMT"; Server = "nginx/1.4.6 (Ubuntu)"; "Strict-Transport-Security" = "max-age=2592000; includeSubDomains"; "Transfer-Encoding" = Identity; "X-Exception-Class" = "PM\PFTAPIBundle\Exception\MissingAPIParameterException"; "X-Exception-Message" = "Missing parameter: 'reference'"; "X-Frame-Options" = SAMEORIGIN; "X-Processing-Time" = "906.5ms"; } }, NSErrorFailingURLKey=https:///api/mobile/v1/document/get, NSLocalizedDescription=Request failed: unacceptable content-type: text/plain, com.alamofire.serialization.response.error.data=<48545450 20343030 0a4d6973 73696e67 20706172 616d6574 65723a20 27726566 6572656e 636527>}}} Request failed: bad request (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];

}

When I test the api using postman and passing the param in Body as raw data {"reference":{"Id":"User","documentId":"56e0"}} [NOTE: NOT in x-www-form-urlencoded or form-data]. It works and getting response with data.

enter image description here

like image 981
Praveen-K Avatar asked May 25 '26 16:05

Praveen-K


1 Answers

if you are using AFNetworking, try accepting content types explicitly

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

This worked for me

like image 158
Mithra Singam Avatar answered May 27 '26 04:05

Mithra Singam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!