post form_data data with alamofire not work

半世苍凉 提交于 2020-01-06 05:29:07

问题


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!)

in postman success send data with form_data

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

this print(parameter)


回答1:


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.

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.




回答2:


Try following, you are posting data with optional so do unwrap you data.

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
        ]


来源:https://stackoverflow.com/questions/52887152/post-form-data-data-with-alamofire-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!