I'm tryna send a photo along with parameters, but the catch is that I want to send a JSON array to the server. It seems Alamofire doesn't have a method to send a list of Data
, so what's another good alternative to this?
the key part of the problem is:
var encodedTags: [Data] = tags.map({ return $0.data(using: .utf8)!})
mpd.append(encodedTags, withName: key)
within this upload call:
let parameters: [String: Any] = ["username": "TheCooliest", ..., "tags": ["KoolKid", "TheKooliest", "BetterThanKimK"]
...
upload(multipartFormData: { (mpd) in
mpd.append(url, withName: "file", fileName: "weeknd.jpg")
for (key, value) in parameters {
if let tags = value as? [String], key == "tags" {
var encodedTags = tags.map({ return $0.data(using: .utf8)!})
mpd.append(encodedTags, withName: key)
}
}
}
Okay, so I used JSONSerialization
. It converts my list into an Any
object which I convert into Data.
for (key, value) in parameters {
if let tags = value as? [String], key == "tags" {
do {
let json = try JSONSerialization.data(withJSONObject: tags, options: .prettyPrinted)
mpd.append(json as Data, withName: key)
} catch {}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With