-1

I'm trying to make post request with alamofire with body, but I keep having this error code "Message: An error has occurred." I run the same api in obj-c and afnetworking, all works fine. Am I using the wrong way to make parameters?

let parameters: [String: Any] = ["ID": "aaaa@test.com", "PW": "12345", "LoginType": "IDPW", "PushKey": "ios"]
Alamofire.request(loginApi, method: .post, parameters: parameters, encoding: JSONEncoding.default)
            .responseJSON { response in
                print(response)
        }

Body i send using Objective-C & function

     NSDictionary *parameters = @{
                             @"LoginType": @"IDPW",
                             @"ID": @"aaaa@test.com",
                             @"PW": @"12345",
                             @"PushKey":  @"ios"
                             };
     - (void) AsyncHttpRequestWithMethod:(NSString * _Nonnull)method
                      URLString:(NSString * _Nonnull)URLString
                     parameters:(NSDictionary * _Nullable)parameters
                        headers:(NSDictionary * _Nullable)headers
                 uploadProgress:(nullable void (^)(NSProgress * _Nullable uploadProgress)) uploadProgressBlock
               downloadProgress:(nullable void (^)(NSProgress * _Nullable downloadProgress)) downloadProgressBlock
              completionHandler:(nullable void (^)(NSURLResponse * _Nullable response, id _Nullable responseObject,  NSError * _Nullable error))completionHandler {

AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSMutableURLRequest *req = [[AFHTTPRequestSerializer serializer] requestWithMethod:method URLString:URLString parameters:parameters error:nil];

req.timeoutInterval = intTimeInterval;

if (headers && headers.count > 0) {
    NSArray *keys = headers.allKeys;
    for (int i = 0; i < headers.count; i ++) {
        [req addValue:headers[keys[i]] forHTTPHeaderField:keys[i]];
    }
}

[[manager dataTaskWithRequest:req uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
    if (uploadProgressBlock) uploadProgressBlock(uploadProgress);
} downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
    if (downloadProgressBlock) downloadProgressBlock(downloadProgress);
} completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

    NSLog(@"completionHandler......");

    completionHandler(response, responseObject, error);
}] resume];

}

fritz
  • 23
  • 1
  • 9

2 Answers2

1

In your request headers is missing, Please use the following code to get the result.

let headers = ["Content-Type":"application/json"]    
let parameter = ["MemberID": "aaaa@test.com", "MemberPW": "12345", "LoginType": "IDPW", "PushKey": "ios"]

            Alamofire.request(loginApi, method: .post, parameters: parameter, encoding: JSONEncoding.default, headers: headers)
                .responseJSON { (response) in
                    switch response.result {
                    case .success:
                        print("success",response)
                    case .failure(let error):
                        print("failure",error)
                    }
            }

enter image description here

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
  • I add headers in the request and still getting SUCCESS: { Message = "An error has occurred."; } – fritz Feb 22 '19 at 05:11
  • Okay you API call is getting response success block, but there is some issue may occur from API side. – AtulParmar Feb 22 '19 at 05:19
0

Your codes looks good to me.

Without more information it is difficult to identity that what is actual problem.

As per me possible problem is encoding

change

JSONEncoding.default

with other available options like URLEncoding.default

Let me know if it is not working then I will delete my answer

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98