Send array as a parameter in Alamofire API call in Swift

69 Views Asked by At

I have to send this JSON object array parameters as an array and not as [String:Any], following is the array of objects:

 [
      {
        "value": "string",
        "recorded_at": "2024-02-27T06:13:28.102Z",
        "type": "string",
        "unit": "string",
        "note": "string",
        "recorder_method": "device",
        "meta_data": {
          "additionalProp1": "string",
          "additionalProp2": "string",
          "additionalProp3": "string"
        }
      },
      {
        "value": "string",
        "recorded_at": "2024-02-27T06:13:28.102Z",
        "type": "string",
        "unit": "string",
        "note": "string",
        "recorder_method": "device",
        "meta_data": {
          "additionalProp1": "string",
          "additionalProp2": "string",
          "additionalProp3": "string"
        }
      }
    ]

and I have update the code with all the things I can find online, Now I am getting my arrayOfObjects is not in correct format

   let params = [
                "value": dataValue ,
                "recorded_at" : "\(value)",
                "type" : paramValueType,
                "unit" : unitValue,
                "note" : addNote ?? "",
                "recorder_method" : "device"
            ] as [String : Any]
            
             arrayOfObjects = [params]

        var request = URLRequest(url: url! )
        request.httpMethod = "POST"
        request.setValue(strToken, forHTTPHeaderField: "Header")
    
        request.httpBody = try! JSONSerialization.data(withJSONObject: arrayOfObjects)
                  AF.request(request)
            .responseJSON { response in
                switch response.result {
                case .failure(let error):
                    print(error)

                case .success(let responseObject):
                    print(responseObject)

                }
        }
//        

I can make the api call successfully but I cannot get the correct format for the data any help would be appreciated , meanwhile I will keep trying to get a solution

0

There are 0 best solutions below