post请求发送[String:Any]类型的参数和string类型的主体

时间:2017-09-22 12:38:22

标签: ios swift alamofire

我使用alamofire库创建一个帖子请求,我想发送一个参数:

let parameters: Parameters = ["customer_id":customerId!,"customer_new":"0","x-session":getXSession()]

和一个字符串类型的主体,正文:

fbackAnswers string = [{"type_id":"17","value":"4"},{"type_id":"12","value":"TEST"},{"type_id":"14","value":"3"},{"type_id":"19","value":"4"},{"type_id":"16","value":"3"},{"type_id":"13","value":"3"},{"type_id":"18","value":"3"},{"type_id":"15","value":"3"},{"type_id":"2","value":"4"},{"type_id":"11","value":"1"},{"type_id":"1","value":"2"},{"type_id":"8","value":"4"},{"type_id":"6","value":"2"},{"type_id":"4","value":"22-09-2017 - 15:12"},{"type_id":"5","value":"Test"}]

我在下面提出了这个要求:

 static func sendFeedbackResultOldCustomer(customerId: String?,fbackAnswers:String? , completion: @escaping (Bool , String?) ->() ){
            let fbackAns:String! = fbackAnswers!
            let parameters: Parameters = ["customer_id":customerId!,"customer_new":"0","x-session":getXSession()]

            Alamofire.request(APIStrings.feedbackSent, method: HTTPMethod.post, parameters: parameters, encoding: fbackAns, headers: nil).responseJSON { (response) in


                if (success){
                    completion(true, nil)
                }
                else {
                    completion(false, response?.result.error as? String)
                }
            }

它给我一个错误:电话中的额外参数, 如果这不是发送这些类型的请求的方式,请为我更正,如果是,请帮助我找到错误。 感谢

1 个答案:

答案 0 :(得分:0)

试试这个:

似乎parameter是标题,fbackAns是参数。

static func sendFeedbackResultOldCustomer(customerId: String?,fbackAnswers:String? , completion: @escaping (Bool , String?) ->() ){
            let fbackAns:String! = fbackAnswers!
            let parameters: Parameters = ["customer_id":customerId!,"customer_new":"0","x-session":getXSession()]

            Alamofire.request(APIStrings.feedbackSent, method: HTTPMethod.post, parameters: fbackAns, encoding: URLEncoding.default, headers: parameters).responseJSON { (response) in


                if (success){
                    completion(true, nil)
                }
                else {
                    completion(false, response?.result.error as? String)
                }
            }