# Merging Data for POST request

**URL:** <https://forums.swift.org/t/merging-data-for-post-request/63357>\
**Category:** Alamofire\
**Created:** [February 25, 2023, 12:37pm UTC](https://forums.swift.org/t/merging-data-for-post-request/63357 "2023-02-25T12:37:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![caraberdop](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/caraberdop/32/28907_2.png) [@caraberdop](https://forums.swift.org/u/caraberdop)\
**Post date:** [February 25, 2023, 12:37pm UTC](https://forums.swift.org/t/merging-data-for-post-request/63357/1 "2023-02-25T12:37:35Z")

</div>

so I have a struct that I need to send the data as the payload to a POST request. But I also need to append more data to that payload along with the struct. How do I merge the data together.

```swift
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
    }
}

let series = Series(...args)

// Encode
let jsonData = try? JSONEncoder().encode(series)

let apendedData: //Not sure what to type this as = [
    "foo": true,
    "bar": {
        "baz": true
    }
]

/* Expected payload

{
     "id: 1,
     "title": "Random title",
     "overview": "This is an overview",
     "foo": true,
     "bar": {
         "baz": true
     }
}
 */

```

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [February 25, 2023, 3:45pm UTC](https://forums.swift.org/t/merging-data-for-post-request/63357/2 "2023-02-25T15:45:38Z")

</div>

It's not obvious what you are after. E.g. adding additional data right after JSON is one thing, adding it as another field in JSON is another, MIME - yet another, adding extra parameters as part of the query string or headers - yet another again.

---

<div class="post-metadata">

**Author:** ![mtsrodrigues](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mtsrodrigues/32/34169_2.png) [@mtsrodrigues](https://forums.swift.org/u/mtsrodrigues)\
**Post date:** [February 25, 2023, 4:38pm UTC](https://forums.swift.org/t/merging-data-for-post-request/63357/3 "2023-02-25T16:38:35Z")

</div>

> [@caraberdop](#):
>
> so I have a struct that I need to send the data as the payload to a POST request. But I also need to append more data to that payload along with the struct. How do I merge the data together.

Here's an ideia

```swift

struct Series: Codable {
    
    let id: Int
    let title: String
    let overview: String
}

let series = Series(id: 1, title: "title", overview: "overview")

let seriesData = try! JSONEncoder().encode(series)

let seriesJSON = try! JSONSerialization.jsonObject(with: seriesData, options: []) as! [String: Any]

let additionalData = """
{
    "foo": true,
    "bar": {
        "baz": true
    }
}
""".data(using: .utf8)!

let additionalJSON = try! JSONSerialization.jsonObject(with: additionalData, options: []) as! [String: Any]

let finalJson = seriesJSON.merging(additionalJSON, uniquingKeysWith: { (current, _) in current })

let finalString = String(data: try! JSONSerialization.data(withJSONObject: finalJson, options: [.prettyPrinted]), encoding: .utf8)!

print(finalString)

// {
// "bar" : {
// "baz" : true
// },
// "foo" : true,
// "id" : 1,
// "overview" : "overview",
// "title" : "title"
// }

```

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [February 25, 2023, 6:45pm UTC](https://forums.swift.org/t/merging-data-for-post-request/63357/4 "2023-02-25T18:45:00Z")

</div>

It's not necessary to use `JSONSerialization` here unless you're combining arbitrary values together. To stay within the `Encodable` world you can simply use composition with custom encoding. I'd recommend staying structured and generic if you can.

```swift
struct CommonValues<AdditionalValues: Encodable>: Encodable {
    let bar: Baz
    let additionalValues: AdditionalValues

    enum CodingKeys: CodingKey { case bar }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(bar, forKey: .bar)
        try additionalValues.encode(to: encoder)
    }
}

```

That will encode `bar` and `additionalValues` at the same level in the resulting object.

You could also just define container values with arbitrary arity (at least until the compiler gives up):

```swift
struct EncodeMany<First, Second, Third>: Encodable where First: Encodable, Second: Encodable, Third: Encodable {
  let first: First
  let second: Second
  let third: Third

  func encode(to encoder: Encoder) throws {
    try first.encode(to: encoder)
    try second.encode(to: encoder)
    try third.encode(to: encoder)
  }
}

```

---

<div class="post-metadata">

**Author:** ![stardustWK](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stardustwk/32/5402_2.png) [@stardustWK](https://forums.swift.org/u/stardustWK)\
**Post date:** [August 16, 2024, 7:20am UTC](https://forums.swift.org/t/merging-data-for-post-request/63357/5 "2024-08-16T07:20:01Z")

</div>

> [@Jon\_Shier](#):
>
> ```swift
> func encode(to encoder: Encoder) throws {
> try first.encode(to: encoder)
> try second.encode(to: encoder)
> try third.encode(to: encoder)
> }
> 
> ```

well, Merging multiple encodables in this way may cause crashes because multiple different container types from the same encoder. see [Aggregating two Encodable’s](https://forums.swift.org/t/aggregating-two-encodables/52515)
