使用alamofire发布form_data数据不起作用 [英] post form_data data with alamofire not work

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

问题描述

我想从我的应用程序
发送数据和图像,但不发送此代码

i want to send data and image from my apps but not send with this code

let parameters: [String: AnyObject] = [
                "latitude" : lat as AnyObject,
                "longitude" : lng as AnyObject,
                "lokasi" : txtAlamat.text as AnyObject,
                "keterangan" : txtKeterangan.text as AnyObject,
                "jenis_absen" : absenIndex as AnyObject
            ]
            let requestUrl = "https://link.co/link_me"

            Alamofire.request(requestUrl, method: .post, parameters: parameters, encoding: JSONEncoding.default).authenticate(user: username!, password: password!).responseJSON { response in
                    print(response)
                if let error = response.error{
                    self.hideLoading()
                    _ = SweetAlert().showAlert("Warning", subTitle:error.localizedDescription, style: AlertStyle.error, buttonTitle:"Dismiss")
                    return
                }
                debugPrint(response.result.value!)
                let parsedResult = JSON(response.result.value!)

邮递员成功发送带有form_data

in postman success send data with form_data

,我想添加图像,但我不知道

and i want to add image to, but i don't know how

此打印(参数)

this print(parameter)

推荐答案

如果您要发送参数和数据(例如例如),您必须使用上传请求(Alamofire.upload(...)),而不是常规请求。

If what you want is to send parameters and data (like an image for example), you MUST use an UPLOAD request (Alamofire.upload(...)), instead of the regular request.

上传请求使用NSData(字节)填充请求正文,因此您必须在 multipartFormData 正文中序列化参数和图像。

The Upload request uses NSData (bytes) to fill the request body, so you'll have to serialize the parameters and the image in the multipartFormData body.

我希望这对您有所帮助在这里澄清问题。

I hope this helped you a little bit, to clarify the problem here.

这篇关于使用alamofire发布form_data数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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