I'm making a request to remote server using RESTful API. I create a dictionary then I use JSONSerialization to serialize it to Data. The problem is when serialize date time dd/mm/yyyy it automatically add a \ character. This problem may be due to the convert from Dictionary to Data or Data to String. I don't know exactly.
How to remove this \ character
Below are my code on app:
var dic = [String : String]()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy hh:mm:ss"
let current : String = dateFormatter.string(from: now)
dic["RequestAt"] = current
// others set key-value
do {
var request = URLRequest(url: URL(string: DOMAIN_NAME)!)
request.httpMethod = "POST"
request.timeoutInterval = 3
request.addValue("application/json charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json charset=utf-8", forHTTPHeaderField: "Accept")
let d = try JSONSerialization.data(withJSONObject: dic, options: [])
let str = String.init(data: d, encoding: .utf8)
print(str ?? "NOTHING") // Same result as server's receive
request.httpBody = d
let session = URLSession.shared
session.dataTask(with: request) { data, response, err in
//handle callback
}.resume()
} catch let error {
print(error.localizedDescription)
}
But server-side receive below json:
{
"RequestAt":"08\/04\/2017 11:42:03",
....
}
Thank you for your support.