I am using the below post method to send JSON array, Please help to me solved below issue?
I need post json data like below ;
key = punchdata
{
"maids" : "649",
"misReason" : "test",
"uId" : "20",
"aDate" : "2020-04-24",
"punchArray" : [
{
"in_time" : "14:10:00",
"in_date" : "2020-04-24",
"out_date" : "2020-04-24",
"out_time" : "19:00:00"
}
]
}
Below I attached the postman request and response, please check ;
func sendJSONArrayToPost() {
let params2 = ["punchdata" : ["uId": "20",
"aDate":"2020-04-24",
"maids":"649",
"misReason":"test",
"punchArray": [
[
"in_date": "2020-05-07",
"in_time": "10:00:00",
"out_date": "2020-05-07",
"out_time": "13:00:00"
]]]] as [String : Any]
let url = "https://iiplccr.hrgird.com/owner/hrmessapi/applymissingPunch"
Alamofire.request(url, method: .post, parameters: params2, encoding:JSONEncoding.default, headers: nil)
.responseJSON { response in
debugPrint(response)
if let data = response.result.value{
if (data as? [String : AnyObject]) != nil{
if let dictionaryArray = data as? Dictionary<String, AnyObject?> {
if dictionaryArray.count > 0 {
var resCode = Int()
var resMessage = ""
if let success = dictionaryArray["success"] as? Int{
resCode = success
}
if let Msg = dictionaryArray["Msg"] as? String{
resMessage = Msg
}
}
}
}
}
}
}
Other Way :
let jsonDataDict: NSMutableDictionary = NSMutableDictionary()
let arrObj: NSMutableDictionary = NSMutableDictionary()
let jsonArrayDict: NSMutableArray = NSMutableArray()
jsonDataDict.setValue("\(mLoginUserId)", forKey: "uId")
jsonDataDict.setValue(dateArray[0], forKey: "aDate")
jsonDataDict.setValue(maidStr, forKey: "maids")
jsonDataDict.setValue(resason, forKey: "misReason")
for item in self.MissingPunchListArray {
arrObj.setValue(item.inDate, forKey: "in_date")
arrObj.setValue(item.inTime, forKey: "in_time")
arrObj.setValue(item.outDate, forKey: "out_date")
arrObj.setValue(item.outTime, forKey: "out_time")
jsonArrayDict.add(arrObj)
}
jsonDataDict.setValue(jsonArrayDict, forKey: "punchArray")
below is another function I am trying to do ;
let url = "https://iiplccr.hrgird.com/owner/hrmessapi/applymissingPunch"
var request = URLRequest(url: NSURL(string: url)! as URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try! JSONSerialization.data(withJSONObject: ["punchdata": jsonDataDict])
Alamofire.request(request)
.responseJSON { response in
switch response.result {
case .failure(let error):
print("error: \(error)")
if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
print("responseString: \(responseString)")
}
case .success(let responseObject):
print("responseObject: \(responseObject)")
}
}
I get below error:
[Data]: 37025 bytes
[Result]: FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))