Extending Alamofire with things like Content-MD5

Can anyone suggest some pointers on how to extend alamofire with headers like Content-MD5 to be set right before submitting?

+3


source to share


1 answer


This is a bit old question, but I had the same problem and solved it using the following code:

        Alamofire.request(.POST, "your-url", parameters: params, encoding: .Custom({(convertible, paramsOptinal) in
        guard let params = paramsOptinal else {return (convertible.URLRequest, NSError(domain: "", code: -1, userInfo: nil))}
        let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest

        let json = try! NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.PrettyPrinted)
        let jsonMd5 = self.MD5(json).base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
        mutableRequest.setValue(jsonMd5, forHTTPHeaderField: "Content-MD5")

        let contentType = "application/json; charset=utf-8"

        mutableRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
        mutableRequest.HTTPBody = json

        return (mutableRequest, nil)}
        )).responseString {response in
        print(response)
    }

      



Hope you find this helpful.

Omer

0


source







All Articles