Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire 4.0, Swift 3 Post params not being passed

When I upgraded to latest everything (Alamo 4, Swift 3 and XC 8) the following stopped posting parameters and I dont have a clue why...

let params = ["stripeToken": token.tokenId,
              "name": name,
              "email": email
             ]
    Alamofire.request(requestString, method: .post, parameters: params, encoding: JSONEncoding.default)
        .responseJSON { (response) in
            if response.result.value is NSNull {
                return
            }
like image 982
Mark Avatar asked Oct 01 '16 01:10

Mark


2 Answers

I had a similar issue, I changed encoding from JSONEncoding.default to URLEncoding.httpbody or encoding: URLEncoding.default

Alamofire.request(URL, method: .post, parameters: params, encoding: URLEncoding.httpBody).responseJSON { response in

    if let data = response.data {
        let json = String(data: data, encoding: String.Encoding.utf8)
        print("Response: \(json)")
    }
}
like image 119
Steven Roseman Avatar answered Sep 28 '22 12:09

Steven Roseman


I have the same issue and finally fixed it. URLEncoding.httpBody didn't work for me... but URLEncoding.default did.

So I changed JSONEncoding.default to URLEncoding.default.

It's now passing the parameters to the backend.

Alamofire.request(loginURL, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil)
like image 36
Ross Avatar answered Sep 28 '22 12:09

Ross