In your example, Alamofire.request(.POST, "http://mywebsite.com/post-request", parameters: ["foo": "bar"]) already contains the string "foo = bar" as its body. But if you really need a custom format string. You can do it:
Alamofire.request(.POST, "http://mywebsite.com/post-request", parameters: [:], encoding: .Custom({ (convertible, params) in var mutableRequest = convertible.URLRequest.copy() as NSMutableURLRequest mutableRequest.HTTPBody = "myBodyString".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) return (mutableRequest, nil) }))
Note: parameters must not be nil
UPDATE (Alamofire 4.0, Swift 3.0):
In Alamofire 4.0, the API has changed. Therefore, for custom coding, we need a value / object that conforms to the ParameterEncoding protocol.
extension String: ParameterEncoding { public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest { var request = try urlRequest.asURLRequest() request.httpBody = data(using: .utf8, allowLossyConversion: false) return request } } Alamofire.request("http://mywebsite.com/post-request", method: .post, parameters: [:], encoding: "myBody", headers: [:])
Silmaril Feb 16 '15 at 23:32 2015-02-16 23:32
source share