小编典典

Alamofire 4.0上传MultipartFormData标头

swift

我们如何向Alamofire 4.0的上传功能添加身份验证标头?

下面是示例代码,但是我看不到向函数添加标题的方法。

Alamofire.upload(
    multipartFormData: { multipartFormData in
        multipartFormData.append(unicornImageURL, withName: "unicorn")
        multipartFormData.append(rainbowImageURL, withName: "rainbow")
    },
    to: "https://httpbin.org/post",
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .failure(let encodingError):
            print(encodingError)
        }
    }
)

先前版本的alamofire支持直接添加标头,但不支持新标头。有任何想法吗?


阅读 331

收藏
2020-07-07

共1个答案

小编典典

我找到了解决方案。

Alamofire.upload(multipartFormData:{ multipartFormData in
         multipartFormData.append(unicornImageURL, withName: "unicorn")
         multipartFormData.append(rainbowImageURL, withName: "rainbow")},
       usingThreshold:UInt64.init(),
       to:"https://httpbin.org/post",
       method:.post, 
       headers:["Authorization": "auth_token"], 
       encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .failure(let encodingError):
            print(encodingError)
        }
    })

希望对您有帮助。

2020-07-07