简体   繁体   中英

Post method request Alamofire (responseObject)

I want to create Alamofire POST request but I get failure response , I dont know the error in mapping object or in the request

   func loginUser() {
        let URL = ..... + "/Login"
        let params2 = "{\"MobileNumber\":\"\(MobileNumber.text!)\"}"
        Alamofire.request(URL, method: .post, parameters: [:], encoding: params2, headers: [
            "Content-Type": "application/json"]).responseObject { (response: DataResponse<Login>) in
                if(response.result.isFailure){
                  print ("failure")
                    print (response.result.description)
                    return
                }
                else{
                    print(response.result)   // result of response serialization
                    self.LoginUser = response.result.value
                }

        }

It looks like you are setting encoding property of the request method to your param2 variable.

Try setting parameter to param2 and update encoding to use .default :

Alamofire.request(URL, method: .post, parameters: param2, encoding: .default, headers: ...

You did not pass proper values into required fields. Use the corrected version below hope it helps

    func loginUser() {
            let URL = ..... + "/Login"
            let params2: [String: Any] = ["MobileNumber" : "\(MobileNumber.text!)"]"
let header = ["Content-Type": "application/json"]
            Alamofire.request(URL, method: .post, parameters: params2, encoding: JSONEncoding.default, headers: header).responseObject { (response: DataResponse<Login>) in
                    if(response.result.isFailure){
                      print ("failure")
                        print (response.result.description)
                        return
                    }
                    else{
                        print(response.result)   // result of response serialization
                        self.LoginUser = response.result.value
                    }

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM