Reputation: 515
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
}
Upvotes: 7
Views: 10223
Reputation: 239
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)")
}
}
Upvotes: 18
Reputation: 609
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)
Upvotes: 7
Reputation: 16643
Everything works exactly as it should. Here's a quick example demonstrating that fact.
func testPostingJSON() {
let urlString = "https://httpbin.org/post"
let params: Parameters = [
"stripeToken": "token_id",
"name": "cnoon",
"email": "[email protected]"
]
let expectation = self.expectation(description: "should work")
Alamofire.request(urlString, method: .post, parameters: params, encoding: JSONEncoding.default)
.responseJSON { response in
if let json = response.result.value {
print("JSON: \(json)")
} else {
print("Did not receive json")
}
expectation.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil)
}
Hopefully that example helps you pinpoint the issue. Cheers. 🍻
Upvotes: 1