如何使用Alamofire快速发布请求json正文?

时间:2020-01-27 13:10:29

标签: ios swift xcode alamofire

我需要在json主体中为我的应用后端发出POST请求,但响应返回失败。我假设我的json格式或编码错误,但是我无法弄清楚问题出在哪里。我尝试了许多不同的解决方案,但找不到有效的解决方案。谁能看到我的代码的哪一部分负责失败?

let headers: HTTPHeaders = [
    "accept": "application/json",
    "content-type": "application/json",
    "authorization": self.authValue,
    "x-iyzi-rnd": self.randomString,
    "cache-control": "no-cache"
]


let parameters: [String: Any] = [


        "price": "1.0",
        "paidPrice": "1.1",
        "paymentChannel": "mobile_ios",
        "paymentCard": [
            "cardHolderName": "card_name",
            "cardNumber": "card_no",
            "expireYear": "2030",
            "expireMonth": "09",
            "cvc": "123"
        ],
        "buyer": [
            "id": "123123123",
            "name": "john",
            "surname": "doe",
            "identityNumber": "12345678902",
            "email": "johndoe@gmail.com",
            "registrationAddress": "nidakulegöztepemerdivenköymahborasokno1",
            "city": "istanbul",
            "country": "turkey",
            "ip": "192.168.1.82"
        ],
        "shippingAddress": [
            "address": "nidakulegöztepemerdivenköymahborasokno1",
            "contactName": "janedoe",
            "city": "istanbul",
            "country": "turkey"
        ],
        "billingAddress": [
            "address": "nidakulegöztepemerdivenköymahborasokno1",
            "contactName": "janedoe",
            "city": "istanbul",
            "country": "turkey"
        ],
        "basketItems": [
            [
                "id": "321",
                "price": "0.3",
                "name": "binocular",
                "category1": "collectibles",
                "itemType": "physical"
            ],
            [
                "id": "432",
                "price": "0.5",
                "name": "gamecode",
                "category1": "game",
                "itemType": "virtual"
            ],
            [
                "id": "543",
                "price": "0.2",
                "name": "usb",
                "category1": "electronics",
                "itemType": "physical"
            ]
        ],
        "currency": "try"


]


Alamofire.request("https://api.iyzipay.com/payment/auth", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON(completionHandler: {
    response in response

    let jsonResponse = response.result.value as! NSDictionary
    print(jsonResponse)



})

有没有其他方法可以发布请求json正文?

0 个答案:

没有答案