2
votes

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

1
You can see that the postman screenshot shows the request is text/json, not text/plain.Avi
@Avi Thanks, But I am setting the content type [theRequest setValue:mACCEPTVALUE forHTTPHeaderField:mCONTENTTYPELABEL]; where mACCEPTValUE is text/jsonPraveen-K
So your question is really how to get AF to send your request as text/json, because the error you posted clearly shows that it isn't.Avi
@Avi Yes Avi. I am passing parameter {"reference":{"sheetId":"User","documentId":"56e02ac1f045d90f508b45fb"}} as raw dataPraveen-K
I see where it's going wrong. It's not the request that's wrong, it's the response. You are expecting a JSON response, but you're getting a 400 response which includes text explaining the error. If you decode the returned data, you get HTTP 400 Missing parameter: 'reference'.Avi

1 Answers

2
votes

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