添加 headers 到 Alamofire multiPartFormData 上传
Adding headers to Alamofire multiPartFormData upload
在尝试解决这个问题时遇到了困难。 Post 使用 Alamofire (4.4.0) 的请求和我的 header 在大多数情况下都可以正常工作,但是这次我需要发送一个字符串字典和一个视频。
视频是通过 UIImagePickerController 录制的,最终进入字典的信息是通过一系列滑块、开关和文本字段创建的。
给我麻烦的block如下:
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)
}
}
上面的代码给出了:
"Thread 1: signal SIGABRT"
控制台输出:
"Could not cast value of type 'Foundation.URLRequest' to '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 - 无效的访问令牌。这是意料之中的,因为我无法通过这个新请求在 header 中传递一个。
如果有人可以阐明如何执行上传 multiPartFormData 请求 和 包含用于身份验证的 header,我将不胜感激。
我刚刚在我的工作代码中添加了您的 Headers。您可以用它替换您的代码。
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( _):
}
}
})
在尝试解决这个问题时遇到了困难。 Post 使用 Alamofire (4.4.0) 的请求和我的 header 在大多数情况下都可以正常工作,但是这次我需要发送一个字符串字典和一个视频。
视频是通过 UIImagePickerController 录制的,最终进入字典的信息是通过一系列滑块、开关和文本字段创建的。
给我麻烦的block如下:
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)
}
}
上面的代码给出了: "Thread 1: signal SIGABRT"
控制台输出: "Could not cast value of type 'Foundation.URLRequest' to '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 - 无效的访问令牌。这是意料之中的,因为我无法通过这个新请求在 header 中传递一个。
如果有人可以阐明如何执行上传 multiPartFormData 请求 和 包含用于身份验证的 header,我将不胜感激。
我刚刚在我的工作代码中添加了您的 Headers。您可以用它替换您的代码。
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( _):
}
}
})