0

How I need to post the request: How I need to post the request

How it looks in the database: how it looks in the database

Hello, I am working on a food app, I need to send the order details to the database. To do that, I have created a data type like

class orderModel : NSObject {

var menu_name : String = ""

var quantity : Int = 0

var price : Double = 0.0

var menu_id : String = ""

init?(menu_name : String,menu_id : String, price : Double,  quantity : Int) {

    guard !menu_id.isEmpty else{
        return nil
    }

    self.menu_name = menu_name
    self.menu_id = menu_id
    self.price = price
    self.quantity = quantity

  }

}

this. I'm then placing the order with,

 let parameters : Parameters = [
                    "custid" : defaults.object(forKey: "userid")!,
                    "carttotal" : Int(totalPrice),
                    "cartitems" : ordersArray,
                    "deliverytime" : timeSlot,
                    "custaddress" : custAdd!,
                    "areacode" : defaults.object(forKey: "areaCode")!
                ]

    Alamofire.request(orderPlaceURL, method: .post, parameters: parameters).responseJSON { response in

        print((response.result.value)!)

    }
    totalPrice = 0.0
    ordersArray.removeAll()

The posted data should look like the first image, but when it's entered into the database, it shows like the second image. Instead of the array elements, just the word "Array". Can anyone please suggest me what to do?

Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
K. Mitra
  • 483
  • 7
  • 17
  • use `JSONSerialization` to add your array in form of parameter –  Feb 12 '18 at 05:28
  • okay.. completely forgot to serialise the json data.. – K. Mitra Feb 12 '18 at 05:51
  • @K.Mitra Check out this https://stackoverflow.com/questions/48495353/how-to-send-multiple-json-objects-as-a-stream-using-alamofire/48537265#48537265 – Rocky Feb 12 '18 at 08:58
  • thanks.. I used Codable to get a string value from my modeled data and then passed the string in the post parameter.. thanks for the suggestions.. – K. Mitra Feb 13 '18 at 08:39

1 Answers1

1

Alamofile Parameters object is of type [String: Any], which is not what you want. Instead make an array of multiple dictionaries and pass that to this function :

func uploadDataToServer(endPoint: String, dataToUpload: [[String: Any]], completionHandler:@escaping (Bool) -> ()){

    let url = Config.BASE_URL + endPoint

    var request = URLRequest(url: URL.init(string: url)!)
    request.httpMethod = "POST"

    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    let dataToSync = dataToUpload
    request.httpBody = try! JSONSerialization.data(withJSONObject: dataToSync)

    Alamofire.request(request).responseJSON{ (response) in

        print("Success: \(response)")
        switch response.result{
        case .success:
            let statusCode: Int = (response.response?.statusCode)!
            switch statusCode{
            case 200:
                completionHandler(true)
                break
            default:
                completionHandler(false)
                break
            }
            break
        case .failure:
            completionHandler(false)
            break
        }
    }
}

Your array should be :

var uploadArray = [[String: Any]]()
let ordersDictionary = [
                    "custid" : defaults.object(forKey: "userid")!,
                    "carttotal" : Int(totalPrice),
                    "cartitems" : ordersArray,
                    "deliverytime" : timeSlot,
                    "custaddress" : custAdd!,
                    "areacode" : defaults.object(forKey: "areaCode")!
                ]

uploadArray.append(ordersDictionary)

Then pass uploadArray to above function.

Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50