10

I updated my project to Swift 3 and Alamofire 4. I was using custom Encoding, but it's changed to other encoding methods. I am not able to find the alternative/equivalent to this:

alamoFire.request(urlString, method: HTTPMethod.post, parameters: [:], encoding: .Custom({
        (convertible, params) in

        let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
        let data = (body as NSString).data(using: String.Encoding.utf8)
        mutableRequest.httpBody = data
        return (mutableRequest, nil)

    }), headers: headers()).responseJSON { (responseObject) -> Void in

        switch responseObject.result {
        case .success(let JSON):
            success(responseObject: JSON)

        case .failure(let error):
            failure(error: responseObject)
        }
    }

I also tried by making URLRequest object and simple request its also giving me errors

var request = URLRequest(url: URL(string: urlString)!)
    let data = (body as NSString).data(using: String.Encoding.utf8.rawValue)
    request.httpBody = data
    request.httpMethod = "POST"
    request.allHTTPHeaderFields = headers()

    alamoFire.request(request).responseJSON { (responseObject) -> Void in

        switch responseObject.result {
        case .success(let JSON):
            success(JSON)

        case .failure(let error):
            failure(responseObject, error)
        }
    }

Do point me in some direction, how to attach httpbody with the Alamofire 4

Raheel Sadiq
  • 9,847
  • 6
  • 42
  • 54

2 Answers2

12

Try this method?

Alamofire.request(url, method: HTTPMethod.post, parameters: parameters, encoding: URLEncoding.httpBody, headers: nil).responseObject(completionHandler: { (response : DataResponse<T>)  in

})
  • This should be the accepted answer as does not require custom implementation of `ParameterEncoding` – mosn Jul 02 '17 at 08:01
11

In Alamofire 4.0 you should use ParameterEncoding protocol. Here is an example, which makes any String UTF8 encodable.

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
  • 4,241
  • 20
  • 22
  • how does enconding: "myBody" relate to any other code in this example? this answer is uncomplete althought accepted. – CptEric Jan 23 '17 at 10:21
  • "myBody" is just an example string, which represents body of your request. By default you can't use `String` as `coding` parameter, but with a help of `extension` in my example you can. – Silmaril Jan 23 '17 at 10:27
  • Interesting. There should be a more elegant way of providing a raw enconding than extending String's class, thanks for your quick response. – CptEric Jan 23 '17 at 10:31