Alamofire 4.0, Swift 3 Subsequent parameters not passed

When I updated everything to the last (Alamo 4, Swift 3 and XC 8), the following stopped publishing options, and I don't know 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
            }
+4
source share
3 answers

I had a similar problem, I changed the encoding from JSONEncoding.default to URLEncoding.httpbody or the 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)")
    }
}
+14
source

I have the same problem and finally fixed. URLEncoding.httpBody did not work for me ... but URLEncoding.default did.

So, I changed JSONEncoding.default to URLEncoding.default .

Now it passes the parameters to the server.

Alamofire.request(loginURL, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil)
+6

, . , .

func testPostingJSON() {
    let urlString = "https://httpbin.org/post"

    let params: Parameters = [
        "stripeToken": "token_id",
        "name": "cnoon",
        "email": "cnoon@alamofire.org"
    ]

    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)
}

, . . 🍻

+1

Source: https://habr.com/ru/post/1656424/


All Articles