I try to upload a video using Alamofire with its upload method - > multipartFormData, but I get an error 401, for the headers, it is assumed that the headers only ask me - > form-data and a token, I have it this way:
func headersData() -> [String: String] {
guard let user = OfflineHandler.shared.getUser() else {
return [ "Content-Type": "multipart/form-data"]
}
return [
"Content-Type": "multipart/form-data",
"Authorization": user.token
]
}
and the headers I add them in the following way:
func requestPOSTUppload(_ route: String, params: [String: AnyObject] = [:],
onComplete: @escaping (SessionManager.MultipartFormDataEncodingResult) -> Void ) {
let threshHold: UInt64 = UInt64("video") ?? UInt64()
Alamofire.upload(multipartFormData: { (formData) in
for (key, value) in params {
multipartFormData.append(paramsData, withName: key, fileName: "file", mimeType: "video/mp4")
let paramsData: Data = NSKeyedArchiver.archivedData(withRootObject: value)
formData.append(paramsData, withName: key)
}
}, usingThreshold: threshHold, to: route, method: .post, headers: headersData(), encodingCompletion: onComplete)
}
then I add the parameters from another function
func uploadVideo(accountId: String, file: NSData, post: PostSummary,
onComplete: @escaping(SessionManager.MultipartFormDataEncodingResult) -> Void) {
let path = route("video")
let params = [
"accountId": accountId,
"file": file,
"post": post.getParamsVideo()] as [String: AnyObject]
requestPOSTUppload(path, params: params, onComplete: onComplete)
}
and when calling the function I do the following
func uploadVideo() {
guard
let account = OfflineHandler.shared.getUser()?.getSelectAccount(),
let post = post,
let file = video else { return }
api.uploadVideo(accountId: account.id, file: file, post: post) { (result) in
switch result {
case .success(request: let request, streamingFromDisk: _, streamFileURL: _):
request.response(completionHandler: { (dataResponse) in
guard let response = dataResponse.response else { return }
switch response.statusCode {
case 200:
print("success")
self.navigationController?.popToRootViewController(animated: true)
case 300 ... 400:
print(dataResponse.response)
case 401 ... 500:
print(dataResponse.response)
default:
break
}
})
case .failure(let error as NSError):
print(error.userInfo)
}
}
}
but I get the error 401 of the headers along that is happening to me ?? thank you, advance!