Mr.G Mr.G - 2 months ago 15
Swift Question

Create a dictionary in swift with alamofire

Im trying to create a web request using alamofire in swift , by request object should be like this

{
"warranty": 0,
"descriptions": "string",
"product_name": "string",
"purchase_date": "23/10/2016",
"product_image": "string",
"product_receipt": "string",
"serial_number": "string",
"barcode_image": "string",
"serial_number_image": "string",
"product": {
"id": 1
},
"user": {
"id": 12
}
}


So in order to get this i have put my code as this

let parameters :[String:AnyObject] = [
"warranty":product.warrenty,
"descriptions":product.longDescription,
"product_name":product.initialName,
"purchase_date":product.purchaseDate,
"serial_number":product.serialCode,
"product": [
"id":product.id
],
"user": [
"id":userDefaults.getCustomerId()
]
]


But i when i do the request it seems that server doesn't accept this format , probably the way i assign

"product": {
"id": 1
},
"user": {
"id": 12
}


is not correct , what is the issue in here ? can some one point me the issue that i do here

Answer

I had the same issue and the problem was, i was missing the encoding option which you need to set .JSON. Match the below line with yours.

Alamofire.request(.POST, strURL, parameters: parameter as? [String : AnyObject], encoding: .JSON).responseJSON { (response: Response<AnyObject, NSError>) in