How to send a POST request with BODY in swift

If you are using Alamofire v4.0+ then the accepted answer would look like this: let parameters: [String: Any] = [ “IdQuiz” : 102, “IdUser” : “iosclient”, “User” : “iosclient”, “List”: [ [ “IdQuestion” : 5, “IdProposition”: 2, “Time” : 32 ], [ “IdQuestion” : 4, “IdProposition”: 3, “Time” : 9 ] ] ] Alamofire.request(“http://myserver.com”, method: … Read more

Swift Alamofire: How to get the HTTP response status code

For Swift 3.x / Swift 4.0 / Swift 5.0 users with Alamofire >= 4.0 / Alamofire >= 5.0 response.response?.statusCode More verbose example: Alamofire.request(urlString) .responseString { response in print(“Success: \(response.result.isSuccess)”) print(“Response String: \(response.result.value)”) var statusCode = response.response?.statusCode if let error = response.result.error as? AFError { statusCode = error._code // statusCode private switch error { case .invalidURL(let … Read more

How to parse JSON response from Alamofire API in Swift?

The answer for Swift 2.0 Alamofire 3.0 should actually look more like this: Alamofire.request(.POST, url, parameters: parameters, encoding:.JSON).responseJSON { response in switch response.result { case .Success(let JSON): print(“Success with JSON: \(JSON)”) let response = JSON as! NSDictionary //example if there is an id let userId = response.objectForKey(“id”)! case .Failure(let error): print(“Request failed with error: \(error)”) … Read more