Home > front end >  How to send data into post request parameters for the following JSON data?
How to send data into post request parameters for the following JSON data?

Time:01-06

I have checkboxes required for product return on a page with more than one product, and when I press a checkbox, I want to store the id and quantity of the selected product in the items in the post parameter that I want to send.But, I cannot send the correct data. How can I send correct data?

{
 "reasonId" : "001",
 "cancel" : "true",
 "description": "" ,
 "items": [
    {
        "id": "874a8064-bebf-41c3-98a8-6ac39a54156a",
        "quantity" : 480
    },
    {
        "id": "d7af8722-58cb-4bd0-9927-47de44ba2e0b",
        "quantity" : 2
    },
    {
        "id": "f799d66e-cfcd-4f2b-9603-1facf2fedbea",
        "quantity" : 1
    },
    {
        "id": "5ea0c31f-952a-4fbc-b623-9086030193ad",
        "quantity" : 17
    }
]
}

I can print the id and quantity of the selected product.

private func createCheckBox(_ model : OrderDetailItems, tag: Int) -> UIView {
let rootView = UIView()
returnItemCount = model.definition?.count
var checkBox: ViewCheckLabel!
checkBox = ViewCheckLabel(text: "",
                            range: "",
                            action: {
    // CHECKK SELECT OR NOT SELECT
    checkBox.buttonCheck.checkboxAnimation {
        if checkBox.buttonCheck.isSelected {
            print(model.quantity)
            print(model.id)
        } else {
            
        }
    }
})

This is my post request

  func cancelOrderApi() {
        if let parentVC = parentViewController as? MyProfileViewController {
            parentVC.startActivityIndicator(mainView: parentVC.view)
            
                        let parameters : Parameters = [
                            "reasonId" : reasonId,
                            "cancel" : isOrdrReturnable,
                            "description": "",
                            "items" : ""
                        ]
            
            NetworkLayer.request(Router.returnOrder(parameters, orderReturnId ?? "")).responseDecodable(of: OrderCancel.self) { (response) in
                            
                            switch response.result {
                                case .failure(let error):
                                    Logger.shared.debugPrint("Error while fetching tags: \(String(describing: error))")
                                    return
                                case .success(let response):
                                    if response.code == 200 {
                                        parentVC.showAlert(mesg: response.message ?? "Siparişiniz İade edilmiştir", title: "Başarılı")
                                    } else {
                                        parentVC.showAlert(mesg: response.message ?? "", title: "Hata")
                                        Logger.shared.debugPrint(response.message ?? "")
                                    }
                                    
                                    Logger.shared.debugPrint(response)
                            }
                            DispatchQueue.main.async {
                                parentVC.stopActivityIndicator()
                                parentVC.profileTableView.reloadData()
                            }
                        }
                    }
    }

CodePudding user response:

I am not entirely sure what you want to achieve, since the question is unclear, however here is a tip how to create Data object (can be later inserted into Url request) from your JSON file. Firstly manually copy your JSON file somewhere to your project and give it name yourJSONFile , then you can create Data object with create createDataFromJson() function.

func createDataFromJson() -> Data? {
    if let path = Bundle.main.path(forResource: "yourJSONFile", ofType: "json") {
        do{
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
            return data
        } catch {
            print("catched : \(error.localizedDescription) ❌")
            return nil
        }
    } else {
        return nil
    }
}
  •  Tags:  
  • Related