0

I am trying to send multipart image data using Alamofire with [String:Any] parameters ,i am only able to to post [String:String] parameters only

For [String:String] is doing like https://stackoverflow.com/a/40440371/4466607

but now i have to post like : [ PayLoad ] and i have to send Image with image key which, i am already doing .

In postman it is working fine enter image description here

Question : How can i post image with Dictionary type [String:Any] in Swift Please help

Dhiru
  • 3,040
  • 3
  • 25
  • 69

2 Answers2

0

If your value is Any type then check this may be it helps you.

for (key, value) in params {
  let paramsData:Data = NSKeyedArchiver.archivedData(withRootObject: value)
  formData.append(paramsData, withName: key)
}
Samir Shaikh
  • 547
  • 3
  • 9
  • i am already doing this , i need to post payload of type `[String,Any]` along with my image – Dhiru Jun 04 '19 at 06:20
  • let updateInfo: [String:Any] = ["email":usernameText] for (key, value) in updateInfo { multipartFormData.append(key.data(using: .utf8)!, withName: value as! String) } -try this code – Samir Shaikh Jun 04 '19 at 06:28
  • No , you are appending wrong with data , `withName : key` i know how to append but not working this way , please try then post – Dhiru Jun 04 '19 at 06:55
0

Rough solution:
If you insist on put Image data into a dictionary, you could convert image data to Base64 code, and then put Base64 string in the dictionary. You will submit a big JSON to the server. I don't think it's a good solution.

Better solution:
Another WebApi to submit images data, and that API will return uploaded images URL like https://www.example.com/image1234.jpg, or just return image id like "1234" which can be embedded to a URL lately. Finally, you submit the JSON data with image URL or id like {"image":"https://www.example.com/image1234.jpg"} or {"image":1234}.

Yun CHEN
  • 6,450
  • 3
  • 30
  • 33