I am trying to request a message with the body in quick use of Alamofire.
my json body looks like this:
{ "IdQuiz" : 102, "IdUser" : "iosclient", "User" : "iosclient", "List":[ { "IdQuestion" : 5, "IdProposition": 2, "Time" : 32 }, { "IdQuestion" : 4, "IdProposition": 3, "Time" : 9 } ] }
I am trying to make let list using NSDictionnary, which looks like this:
[[Time: 30, IdQuestion: 6510, idProposition: 10], [Time: 30, IdQuestion: 8284, idProposition: 10]]
and my query using Alamofire looks like this:
Alamofire.request(.POST, "http://myserver.com", parameters: ["IdQuiz":"102","IdUser":"iOSclient","User":"iOSClient","List":list ], encoding: .JSON) .response { request, response, data, error in let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding) println(dataString) }
There is an error in the request, and I believe that the problem is with the Dictionary list, because if I make a request without a list, it works fine, so any idea?
I tried to offer a solution, but I ran into the same problem:
let json = ["List":list,"IdQuiz":"102","IdUser":"iOSclient","UserInformation":"iOSClient"] let data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted,error:nil) let jsons = NSString(data: data!, encoding: NSUTF8StringEncoding) Alamofire.request(.POST, "http://myserver.com", parameters: [:], encoding: .Custom({ (convertible, params) in var mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest mutableRequest.HTTPBody = jsons!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) return (mutableRequest, nil) })) .response { request, response, data, error in let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding) println(dataString) }