How can I post request json body in swift using Alamofire?

狂风中的少年 提交于 2020-01-30 08:08:13

问题


I need to make a POST request in the json body for my app backend but the response returns a failure. I assume my json formatting or encoding is wrong but I can't figure out what the problem is. I have attempted a lot of different solutions but I haven't been able to find one that works. Can anyone see which part of my code is responsible for the failure?

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)



})

Is there any different way to post request json body?

来源:https://stackoverflow.com/questions/59931849/how-can-i-post-request-json-body-in-swift-using-alamofire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!