将标头添加到Alamofire multiPartFormData上传

时间:2017-05-22 05:46:36

标签: swift3 alamofire ios10

在尝试解决这个问题时遇到了困难。使用Alamofire(4.4.0)发布请求,我的标题适用于大多数事情,但这次我需要发送字符串字典和视频。

视频通过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 - 无效的访问令牌。这是预期的,因为我无法通过这个新请求在标题中传递一个。

如果有人能说明如何进行上传,那么会有一个包含用于身份验证的标头我会很感激。

1 个答案:

答案 0 :(得分:3)

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

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