sohail
sohail

Reputation: 1

AFNETWORKING 3.0 Post Request Fail

I sent a request to server using AFNETWORKING 3.0 with the following parameters:

NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];           
[parameters setObject:_texLoginUname.text forKey:@"ent_email"]; 
[parameters setObject:_textLoginPassword.text forKey:@"ent_password"];     
[parameters setObject:uniqueIdentifier forKey:@"ent_device_id"]; 
[parameters setObject:@"2" forKey:@"eat_device_type"]; 
[parameters setObject:@"22" forKey:@"ent_device_token"]; 

Here is my code:

NSString *URL = @"http://optime.in/apps/food_truck/user_api/login";

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[manager POST:URL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
{
    NSLog(@"success!");

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"error: %@", error);
    NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
    NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil];
    NSLog(@"val = %@",serializedData);

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                        message:@"Sorry, try again."
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];

}];

And i got this error:

NSLocalizedDescription=Request failed: not found (404), NSUnderlyingError=0x7be19ef0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"

UserInfo={com.alamofire.serialization.response.error.response=http://optime.in/apps/food_truck/user_api/login } { status code: 404, headers { Connection = "Keep-Alive"; "Content-Length" = 302; "Content-Type" = "text/html; charset=iso-8859-1"; Date = "Tue, 20 Sep 2016 08:12:20 GMT"; "Keep-Alive" = "timeout=5, max=100"; Server = "Apache/2.4.7 (Ubuntu)"; } }, NSErrorFailingURLKey=@"mylink" , NSLocalizedDescription=Request failed: unacceptable content-type: text/html,

Kindly help me to solve this error.

Upvotes: 0

Views: 656

Answers (1)

Jitendra Modi
Jitendra Modi

Reputation: 2394

you should use below Method because AFJSONRequestSerializer is not working is AFNetworking 3.0

This method can work because I use this and only method

NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];           
[parameters setObject:_texLoginUname.text forKey:@"ent_email"]; 
[parameters setObject:_textLoginPassword.text forKey:@"ent_password"];     
[parameters setObject:uniqueIdentifier forKey:@"ent_device_id"]; 
[parameters setObject:@"2" forKey:@"eat_device_type"]; 
[parameters setObject:@"22" forKey:@"ent_device_token"]; 
NSString *URL = @"http://optime.in/apps/food_truck/user_api/login";

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    [manager POST:URL parameters:_parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSLog(@"success!");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error: %@", error);
    }];

Hope this thing works

Upvotes: 2

Related Questions