I'm trying to update the details of a profile using Alamofire multipartFormData. Below is the Postman screenshot of how the request should be.
Below is what I'm trying to do using multipartFormData in Alamofire.
func Post(imageOrVideo : UIImage?){
let headers: HTTPHeaders = [
"Content-type": "multipart/form-data"
]
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "landing.jpeg", mimeType: "image/jpeg")
multipartFormData.append(data: Data, withName: self.Pharmacy_name.text!)
},
to: "url", method: .post , headers: headers)
.response { resp in
print("response is:" , resp)
}
In order for the API call to return "true", all of the above data should be there in the request. I just don't get how to append the textfield values to multipartFormData.append.
I'm new to iOS development therefore, please bear with me. Any help I could get is much appreciated!
Got it fixed! Below is the code.
func Post(imageOrVideo : UIImage?){
let headers: HTTPHeaders = [
/* "Authorization": "your_access_token", in case you need authorization header */
"Content-Type": "multipart/form-data"
]
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "landing.jpeg", mimeType: "image/jpeg")
multipartFormData.append(self.Pharmacy_name.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "name")
multipartFormData.append(self.Address_line1.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "address_line_1")
multipartFormData.append(self.Address_line2.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "address_line_2")
multipartFormData.append(self.Start_time.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "open_at")
multipartFormData.append(self.close_time.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "close_at")
multipartFormData.append(self.contact_number.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "phone_no")
multipartFormData.append("0.00".data(using: .utf8, allowLossyConversion: false)!, withName: "longitude")
multipartFormData.append("0.00".data(using: .utf8, allowLossyConversion: false)!, withName: "latitude")
multipartFormData.append("1".data(using: .utf8, allowLossyConversion: false)!, withName: "id")
multipartFormData.append(self.token.data(using: .utf8, allowLossyConversion: false)!, withName: "api_token")
},
to: "url", method: .post , headers: headers)
.response { resp in
print("response is:" , resp)
}
Hope this helps someone.