I facing some error when i want to send some data on the server. And this the problem is when i converting the image into Base64 code, then it will create some problem. Please help me to resolve this issue. The data could not be in correct format and status code is 500. I don't know how to resolve this.
Error:
<NSHTTPURLResponse: 0x1c043c2c0> { URL: http://192.168.1.58/api/visitor/store } { Status Code: 500, Headers {
"Cache-Control" = (
"no-cache, private"
);
Connection = (
close
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
Date = (
"Mon, 19 Mar 2018 05:11:06 GMT"
);
Server = (
"Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1"
);
"Transfer-Encoding" = (
Identity
);
Vary = (
Authorization
);
"X-Powered-By" = (
"PHP/7.1.1"
);
"X-RateLimit-Limit" = (
60
);
"X-RateLimit-Remaining" = (
59
);
} }
The data couldn’t be read because it isn’t in the correct format.
My Code is :
func apiToSaveData(strURL: String)
{
let myURL = URL(string: strURL)
let request = NSMutableURLRequest(url: myURL!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let token = "Bearer " + strToken
request.setValue(token, forHTTPHeaderField: "Authorization")
var postString: [String : String] = ["" : ""]
if let navController = self.navigationController, navController.viewControllers.count >= 2
{
//Comes from After VerifyOTP after Returning Screen
let viewController = navController.viewControllers[navController.viewControllers.count - 2]
if viewController.restorationIdentifier == "VerifyOTP"
{
postString = ["image": strEncodedImg, "name": tfName.text!, "phone": tfMobile.text!, "email": tfEmail.text!, "otp": strOTP]
}
else if viewController.restorationIdentifier == "VisitorVC"
{
let imgObj = otlBtnTakeImage.imageView?.image
let imgData: Data = UIImagePNGRepresentation(imgObj!)!
let strEncodedImg1 = imgData.base64EncodedString()
print("ENcodedImage: \(strEncodedImg1)")
postString = ["image": strEncodedImg1, "name": tfName.text!, "phone": tfMobile.text!, "email": tfEmail.text!]
}
}
}
You need to first convert your data which is in the form of dictionary into json and send it to server So, add this code into your api function.
func apiToSaveData(strURL: String)
{
let myURL = URL(string: strURL)
let request = NSMutableURLRequest(url: myURL!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let token = "Bearer " + strToken
request.setValue(token, forHTTPHeaderField: "Authorization")
var postString: [String : String] = ["" : ""]
if let navController = self.navigationController, navController.viewControllers.count >= 2
{
//Comes from After VerifyOTP after Returning Screen
let viewController = navController.viewControllers[navController.viewControllers.count - 2]
if viewController.restorationIdentifier == "VerifyOTP"
{
postString = ["image": strEncodedImg, "name": tfName.text!, "phone": tfMobile.text!, "email": tfEmail.text!, "otp": strOTP]
}
else if viewController.restorationIdentifier == "VisitorVC"
{
let imgObj = otlBtnTakeImage.imageView?.image
let imgData: Data = UIImagePNGRepresentation(imgObj!)!
let strEncodedImg1 = imgData.base64EncodedString()
print("ENcodedImage: \(strEncodedImg1)")
postString = ["image": strEncodedImg1, "name": tfName.text!, "phone": tfMobile.text!, "email": tfEmail.text!]
}
}
do {
// pass dictionary to nsdata object and set it as request body
request.httpBody = try JSONSerialization.data(withJSONObject: postString, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
let postTask = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
print(response!)
guard error == nil else {
return
}
guard let data = data else {
return
}
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print("POST Method :\(json)")
if dict["status"] as? String == "1"
{
}
} catch let error {
print(error.localizedDescription)
}
}
postTask.resume()
}