2
votes

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.

1

1 Answers

4
votes

I just added your Headers in my working code. You can replace your code with it.

    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( _):
                    }
                }
        })