iosswiftauthenticationafnetworking

Error:Request failed: bad request (400) login via afnetworking in swift


I am new to swift and trying to login via AFNetworking which is working fine with Postman.

let manager = AFHTTPSessionManager(baseURL: NSURL(string: "****"))
manager.requestSerializer = AFHTTPRequestSerializer()
manager.requestSerializer = AFHTTPRequestSerializer()

let params = ["user":username,"password":password]

manager.POST("/api/LoginApi/appLogin", parameters: params, success:
    {
        requestOperation, response in

        let result = NSString(data: response as! NSData, encoding: NSUTF8StringEncoding)!

        print(result)
    },failure: {
        requestOperation, error in print(error)

Can somebody mark the issue with this code !!


Solution

  • if you are post json with afnetwork 3.0

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager POST:url parameters:parametersDictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"success!");
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"error: %@", error);
        }];