使用Alamofire进行带帖子的登录不起作用

问题描述 投票:0回答:2

您好,我正在尝试与Alamofire一起发表这篇文章,但是我失败了,当我与邮递员一起尝试这篇文章时,我收到了一条成功的消息。

fetch("urlofMyServer", {
    credentials: "include",
    method: "POST",
    body: new URLSearchParams({
      "identity": "[email protected]",
      "credential": "hello123",
      "type": "normal_user",
    }) // content-type has to be application/x-www-form-urlencoded
}).then(() => console.log("success"));

这是我的Alamofire代码,我错了吗?

public func makeLogin(param: [String:String], completion: @escaping ((Bool?) -> Void)){
        let headers: HTTPHeaders = [
            "Content-Type": "application/x-www-form-urlencoded"
        ]
        AF.request(endPoints.login.login, method: .post, parameters: param, encoding: JSONEncoding.default, headers: headers, interceptor: nil).responseJSON { (response) in
            if let status = response.response?.statusCode{
                do {
                    let result = try response.result.get()
                    if let data = result as? [String:String]{
                        print(data)
                        completion(true)
                    }
                } catch{
                    completion(false)
                }
            }else{
                completion(false)
            }
        }
    }

我的参数是:

let info = [
     "identity" : "[email protected]",
     "credential": "hello124",
     "type" : "normal_user"
] as! [String:String]

和我的标题是:

let headers: HTTPHeaders = [
     "Content-Type": "application/x-www-form-urlencoded"
]

body postman

headers postman

params postman

[我正在使用Alamofire 5和Swift 5,重复一次,与邮递员一起收到了一条成功的消息,但是对于Alamofire,该消息是:“缺少必填字段:身份。”

swift alamofire
2个回答
0
投票

只需更改编码:JSONEncoding.defaultString.Encoding.utf8,您就可以开始了!

AF.request(endPoints.login.login, method: .post, parameters: param, encoding: String.Encoding.utf8, headers: headers, interceptor: nil).responseJSON { (response) in

0
投票

尝试更改为URLEncoding.default

© www.soinside.com 2019 - 2024. All rights reserved.