gpt4 book ai didi

swift3 - 将标题添加到 Alamofire multiPartFormData 上传

转载 作者:行者123 更新时间:2023-12-04 12:18:56 25 4
gpt4 key购买 nike

试图弄清楚这一点时遇到了困难。使用 Alamofire (4.4.0) 发布请求,我的 header 在大多数情况下都可以正常工作,但是这次我需要发送字符串字典和视频。

视频是通过 UIImagePickerController 录制的,最终进入字典的信息是通过一系列滑块、开关和文本字段创建的。

给我带来麻烦的块如下:

let headers: HTTPHeaders = [
"X-Access-Token": self.user
]
let requestUrl = try! URLRequest(url: "http://exampleUrl.com", method: .post, headers: headers)

upload(
multipartFormData: { (multipartFormData) in
multipartFormData.append(videoURL!, withName: "video_source")
multipartFormData.append(self.alphaLabel.text!.data(using: .utf8)!, withName: "alpha")
multipartFormData.append(self.betaLabel.text!.data(using: .utf8)!, withName: "beta")
multipartFormData.append(self.gammaLabel.text!.data(using: .utf8)!, withName: "gamma")
multipartFormData.append(self.deltaTextField.text!.data(using: .utf8)!, withName: "delta")
multipartFormData.append(self.epsilonScrollPicker.data(using: .utf8)!, withName: "epsilon")
multipartFormData.append(zetaArray!.data(using: .utf8)!, withName: "zeta")

},
to: requestUrl as! URLConvertible) { encodingResult in
switch encodingResult {
case .success (let upload, _, _):
upload.responseJSON { response in
print(response)
}
case .failure (let encodingError):
print(encodingError)
}
}

上面的代码给出:
“线程 1:信号 SIGABRT”

控制台输出:
“无法将‘Foundation.URLRequest’类型的值转换为‘Alamofire.URLConvertible’。”

为了检查其他一切是否有效,我运行了这个:
let requestUrl "http://exampleUrl.com"

upload(
multipartFormData: { (multipartFormData) in
multipartFormData.append(videoURL!, withName: "video_source")
multipartFormData.append(self.alphaLabel.text!.data(using: .utf8)!, withName: "alpha")
multipartFormData.append(self.betaLabel.text!.data(using: .utf8)!, withName: "beta")
multipartFormData.append(self.gammaLabel.text!.data(using: .utf8)!, withName: "gamma")
multipartFormData.append(self.deltaTextField.text!.data(using: .utf8)!, withName: "delta")
multipartFormData.append(self.epsilonScrollPicker.data(using: .utf8)!, withName: "epsilon")
multipartFormData.append(zetaArray!.data(using: .utf8)!, withName: "zeta")

},
to: requestUrl as! URLConvertible) { encodingResult in
switch encodingResult {
case .success (let upload, _, _):
upload.responseJSON { response in
print(response)
}
case .failure (let encodingError):
print(encodingError)


}
}

如果我使用不同的请求 Url,没有其他参数,我会从我的服务器收到一个 401 - 无效的访问 token 。这是意料之中的,因为我无法在此新请求的 header 中传递一个。

如果有人可以阐明如何上传 multiPartFormData 请求 包含用于身份验证的 header ,我将不胜感激。

最佳答案

我刚刚加了你的 标题在我的工作代码中。你可以用它替换你的代码。

    let yourHeaders: HTTPHeaders = [
"X-Access-Token": "dsfdsfdsf"
]

Alamofire.upload(multipartFormData: { multipartFormData in

for (key, value) in parameter
{
multipartFormData.append(value.data(using: .utf8 )! , withName: key)
}
for videoData in VideoDataArray
{
if videoData
{
multipartFormData.append(videoData , withName: videoParameterName, fileName: "videoName.mp4", mimeType: "video/mp4")
}
}
}, to: "YourApiUrlHere", method: .post, headers : yourHeaders,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
print(upload.progress)

upload.responseJSON { response in

if let JSON = response.result.value
{
print("JSON: \(JSON)")
}
break
case .failure( _):
}
}
})

关于swift3 - 将标题添加到 Alamofire multiPartFormData 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44105428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com