alamofire multipartformdata使用urlrequest [英] alamofire multipartformdata use urlrequest

查看:362
本文介绍了alamofire multipartformdata使用urlrequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Alamo fire多部分表单数据使用请求,例如,我使用上载API

I want to use Alamo fire multipart form data use request, for example, I use upload API

let profile = self.photoView.imageView?.image

    let parameters : [String:String] = [
        "homePageUrl": webURLField.text!,
        "nickName": nickNameField.text!,
        "selfIntro": introField.text!,
        ]

    let uri = Constants.APIURL.changeProfile
    let fileName = "\(nickNameField.text!).jpg"

    Alamofire.upload(multipartFormData: { (multipartFormData) in
         if let imageData = UIImageJPEGRepresentation(profile!, 1.0) {
            multipartFormData.append(imageData, withName: "profile", fileName: fileName, mimeType: "image/jpg")
        }

        for ( key, value ) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: key)
        }

    }, usingThreshold: UInt64.init(), to: uri, method: .patch, headers: Constants.VyrlAPIConstants.getHeader(), encodingCompletion:
        {
            encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                 })

                upload.responseString { response in
                    if ((response.response?.statusCode)! == 200){
                        self.navigationController?.popViewController(animated: true)                            
                    }
                }
            case .failure(let encodingError):
                print(encodingError.localizedDescription)
            }
    })

此代码发送了400条响应从服务器
日志中获取。


PATCH'http://blablabla.com:8080/users/profile ':

PATCH 'http://blablabla.com:8080/users/profile':

Content-Type:multipart / form-data;
boundary = alamofire.boundary.53a2e440bad1fabd X设备:ios
X-APP版本:1.0.0接受语言:ko-kr 400
'http://blablabla.com: 8080 / users / profile'[0.0976 s]:
X-Application-Context:application:dev连接:close
Transfer-Encoding:Identity Date:Thu,13 Jul 2017 01:57:41 GMT

Content-Type: multipart/form-data; boundary=alamofire.boundary.53a2e440bad1fabd X-Device: ios X-APP-Version: 1.0.0 Accept-Language: ko-kr 400 'http://blablabla.com:8080/users/profile' [0.0976 s]: X-Application-Context: application:dev Connection: close Transfer-Encoding: Identity Date: Thu, 13 Jul 2017 01:57:41 GMT

服务器希望接收
http://blablabla.com:8080/users/profile?nickName=abcd&selfIntro=hi%20my%20name%20is。 。
服务器日志是..(成功200代码)

server want to receive http://blablabla.com:8080/users/profile?nickName=abcd&selfIntro=hi%20my%20name%20is.. server log is.. (success 200 code)

curl -X PATCH --header'Content-Type:multipart / form-data'- header
'接受: / '--header'X-APP版本:1.0.0'--header'X设备:ios'
--header'Accept -语言:ko-KR'{ type: formData}' http://blablabla.com:8080 / users / profile?nickName = abcd& selfIntro = hi%20my%20name%20is .... '

curl -X PATCH --header 'Content-Type: multipart/form-data' --header 'Accept: /' --header 'X-APP-Version: 1.0.0' --header 'X-Device: ios' --header 'Accept-Language: ko-KR' {"type":"formData"} 'http://blablabla.com:8080/users/profile?nickName=abcd&selfIntro=hi%20my%20name%20is..'

当然,我想附加?nicName = abcd
使用multiformpart数据

如何使用我们e alamofire代码?

How to use alamofire code?

推荐答案

如果不起作用,则使用这样的参数创建url。

if not work then make url with parameter like this.

 Alamofire.upload(multipartFormData:{ multipartFormData in
        multipartFormData.append(imgData, withName: "profile",fileName: fileName, mimeType: "image/jpg")},
                     usingThreshold:UInt64.init(),
                     to:uri + "?" +nickNameField.text! +"?"+ webURLField.text!+"?"+introField.text!,
                     method:.post,
                     headers:Constants.VyrlAPIConstants.getHeader(),
                     encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .success(let upload, _, _):
                            upload.responseJSON { response in
                                 print(response.result.value)
                            }
                        case .failure(let encodingError):
                            print(encodingError)
                        }
    })

这篇关于alamofire multipartformdata使用urlrequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆