问题
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