Trying to make JSON a payload for a POST request
I have a Model that comes from my API
struct Series: Codable, Hashable, Identifiable {
let id: Int
let title: String
let overview: String
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: Series, rhs: Series) -> Bool {
lhs.id == rhs.id
}
enum CodingKeys: String, CodingKey {
case id
case title
case overview
}
}
Lets say that I have one already made
let series = Series(...args)
I Need the payload for the post request to look like this
{
"id: 1,
"title": "Random title",
"overview": "This is an overview",
"additionalField1": 2,
"additionalField2": false,
"additionalField3": "red"
}
Is there an easier way to do that insted of this?
(in reality a Series has 50+ fields on it that need to get sent in the payload as well)
let payload = [String, (Int, String, Bool)] = [
"id": series.id,
"title": series.title,
"overview": series.overview,
"additionalField1": randomVariable1,
"additionalField2": randomVariable2,
"additionalField3": randomVariable2
]
I know in javascript I would just do something like:
{
...series,
"additionalField1": randomVariable1,
"additionalField2": randomVariable2,
"additionalField3": randomVariable2
}
Ive played around with making another struct
struct AdditionalParams: Codable {
let additionalField1: Int
let additionalField2: Bool
let additionalField3: String
}
But how would I merge AdditionalParams() and Series() together to make the post request?