Any way to get the response body during HTTP errors?

I asked this question on their github page and got an answer from cnoon: swift 2: if let data = response.data { let json = String(data: data, encoding: NSUTF8StringEncoding) print(“Failure Response: \(json)”) } swift 3: if let data = response.data { let json = String(data: data, encoding: String.Encoding.utf8) print(“Failure Response: \(json)”) } https://github.com/Alamofire/Alamofire/issues/1059 I just … Read more

What are Embedded Binaries in Xcode?

Embedded binaries are binary files that are copied to your application bundle when you build the project. Use embedded binaries when your application relies on third-party frameworks so people can use your application without needing those frameworks installed on their machine. Embedded binaries keep users from having to manually install third-party frameworks. Your application uses … Read more

How to return value from Alamofire

As mattt points out, Alamofire is returning data asynchronously via a “completion handler” pattern, so you must do the same. You cannot just return the value immediately, but you instead want to change your method to not return anything, but instead use a completion handler closure pattern. Nowadays, that might look like: func getOrders(completionHandler: @escaping … Read more

Set timeout in Alamofire

For new versions of AF, see https://stackoverflow.com/a/61192412/308315 For old versions, please try this: let request = NSMutableURLRequest(url: URL(string: “”)!) request.httpMethod = “POST” request.setValue(“application/json”, forHTTPHeaderField: “Content-Type”) request.timeoutInterval = 10 // 10 secs let values = [“key”: “value”] request.httpBody = try! JSONSerialization.data(withJSONObject: values, options: []) Alamofire.request(request as! URLRequestConvertible).responseJSON { response in // do whatever you want here … Read more

how to use Alamofire with custom headers

According to the official documentation, modifying the session configuration is not recommended: This is not recommended for Authorization or Content-Type headers. Instead, use URLRequestConvertible and ParameterEncoding, respectively. So an example usage of URLRequestConvertible for authorization would be: enum Router: URLRequestConvertible { static let baseUrlString = “some url string” case Get(query: String) var URLRequest: NSMutableURLRequest { … Read more

Sending json array via Alamofire

You can just encode the JSON with NSJSONSerialization and then build the NSURLRequest yourself. For example, in Swift 3: var request = URLRequest(url: url) request.httpMethod = “POST” request.setValue(“application/json”, forHTTPHeaderField: “Content-Type”) let values = [“06786984572365”, “06644857247565”, “06649998782227”] request.httpBody = try! JSONSerialization.data(withJSONObject: values) AF.request(request) // Or `Alamofire.request(request)` in prior versions of Alamofire .responseJSON { response in switch … Read more

NSURLSession concurrent requests with Alamofire

Yes, this is expected behavior. One solution is to wrap your requests in custom, asynchronous NSOperation subclass, and then use the maxConcurrentOperationCount of the operation queue to control the number of concurrent requests rather than the HTTPMaximumConnectionsPerHost parameter. The original AFNetworking did a wonderful job wrapping the requests in operations, which made this trivial. But … Read more

Send POST parameters with MultipartFormData using Alamofire, in iOS Swift

I found the solution 🙂 finally. We can append data in the request as multipartformdata. Below is my code. Alamofire.upload( .POST, URLString: fullUrl, // http://httpbin.org/post multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(fileURL: imagePathUrl!, name: “photo”) multipartFormData.appendBodyPart(fileURL: videoPathUrl!, name: “video”) multipartFormData.appendBodyPart(data: Constants.AuthKey.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :”authKey”) multipartFormData.appendBodyPart(data: “\(16)”.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :”idUserChallenge”) multipartFormData.appendBodyPart(data: “comment”.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :”comment”) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)