Been having difficulties trying to figure this one out. Post requests using Alamofire (4.4.0) and my headers work fine for most things, however this time I need to send up a dictionary of Strings, as well as a video.
The video is recorded via a UIImagePickerController, and the info that eventually makes it to the dictionary is created via a series of sliders, switches, and text fields.
The block that is given me trouble is as follows:
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)
}
}
The code above gives: "Thread 1: signal SIGABRT"
The console outputs: "Could not cast value of type 'Foundation.URLRequest' to 'Alamofire.URLConvertible'."
To check to see if everything else works, I ran this:
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)
}
}
If I use a different request Url, one without other parameters, I get a 401 - invalid access token from my server. This is to be expected as I am not able to pass one up in the header with this new request.
If anyone can shed light on how to do an upload multiPartFormData request and have a header included for authentication I would appreciate it.