Alamofire 2.0多部分表单数据错误

时间:2015-09-24 15:41:40

标签: swift2 alamofire

此代码在迁移到Swift 2.0和Alamofire 2.0之前工作

manager.upload(requestMethod, NSURL(string: url)!, multipartFormData: { multipartFormData in
            for param in params {
                multipartFormData.appendBodyPart(data: param.1.dataUsingEncoding(NSUTF8StringEncoding)!, name: param.0)
            }

            multipartFormData.appendBodyPart(data: imageData!, name: "file", fileName: "tempImage", mimeType: "image/*") },
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .Success(let upload, _, _):
                    upload.responseJSON { _, response, result in
                        switch result {
                        case .Success(let data):
                            ...
                        case .Failure(let encodingError):
                            ...
                        }
                    }
                case .Failure(let encodingError):
                    ...
                }
        } )

现在行:

upload.responseJson...

始终返回失败" FAILURE:错误域= NSURLErrorDomain代码= -999"取消" "

有人成功地成功使用了Alamofire 2.0中的multipart并知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

在我的情况下,它与其他标题有关。我把像这样的通用标题:

var defaultHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
    defaultHeaders["User-Agent"] = userAgent

    if let ip = ifAddress {
        defaultHeaders["X-Forwarded-For"] = ip
    }

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPAdditionalHeaders = defaultHeaders
    manager = Alamofire.Manager(configuration: configuration)

对于依赖于我在请求中放入的特定条件的其他标头:

if condition {
    headers = ["Accept": contentType]
}

manager!.request(requestMethod, url, parameters: params, headers: headers).responseJSON { response in
…