Alamofire发布请求,身体迅速

时间:2019-02-22 03:30:14

标签: ios swift alamofire

我正在尝试使用带有主体的alamofire发出发布请求,但是我始终收到此错误代码“消息:发生错误”。 我在obj-c和afnetworking中运行相同的api,一切正常。我使用错误的方式来设置参数吗?

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)
        }

我使用Objective-C和功能发送的正文

     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];

}

2 个答案:

答案 0 :(得分:0)

您的请求headers丢失,请使用以下代码获取结果。

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

答案 1 :(得分:0)

您的代码对我来说很好。

没有更多信息,很难确定是什么实际问题。

据我个人可能的问题是encoding

更改

JSONEncoding.default

以及其他可用选项,例如URLEncoding.default

让我知道它是否不起作用,然后我将删除答案

相关问题