I'm using Alamofire with SwiftyJSON to post HTTP requests. An option in the app allows the user to choose a product and a variable option related. I'm using JSONSerialization
to encode the JSON response and post it back with the request.
When testing on Postman I get a positive result while in the app it returns false result. I'v been struggling for hours to find a solution and nothing yet.
The code to post the request:
var optionDictionary = [String:AnyObject]()
var requstParams = [String:String]();
requstParams["product_id"] = self.productId
requstParams["quantity"] = self.quantityValue.text
do {
let jsonSortData = try JSONSerialization.data(withJSONObject: self.optionDictionary, options: [])
let jsonSortString = String(data: jsonSortData, encoding: .utf8)!
requstParams["option"] = jsonSortString
}
catch {
print(error.localizedDescription)
}
NetworkManager.sharedInstance.callingHttpRequest(params:requstParams, apiname:"api/addtoCart", cuurentView: self, method: .post, encoding: JSONEncoding.default){success,responseObject in
if success == 1{
let dict = responseObject as! NSDictionary;
NetworkManager.sharedInstance.dismissLoader()
self.view.isUserInteractionEnabled = true
if dict.object(forKey: "success") as! Int == 1{
let data = dict.object(forKey: "total") as! String
self.tabBarController!.tabBar.items?[3].badgeValue = data.components(separatedBy: " ")[0]
self.navigationCart(cartCount:data.components(separatedBy: " ")[0])
if self.goToBagFlag == true{
self.tabBarController!.selectedIndex = 3
}
}
}
}
Xcode Debugger shows
url https://www.example.com/api/addtoCart
params ["product_id": "23490", "quantity": "1", "option": "{\"2008\":\"7404\"}"]
Success returnData {"success":false,"error":{"option":{"2008":"Option is required!"}}}
While in Postman when I use the following values
{
"quantity": "1",
"product_id": "23490",
"option": {"2008":7403}
}
I get a successful return = true.
I'm confused what am I doing wrong?