4

任何人都可以建议一些关于如何使用标头扩展 alamofire 的指示,例如 Content-MD5 需要在发送之前设置吗?

4

1 回答 1

1

这是一个有点老的问题,但我遇到了同样的问题,我使用以下代码解决了它:

        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)
    }

希望你觉得这很有帮助。

奥马尔

于 2016-06-28T09:48:14.923 回答