Swiftpack.co - vapor/multipart-kit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by vapor.
vapor/multipart-kit 4.5.4
🏞 Parses and serializes multipart-encoded data with Codable support.
⭐️ 124
🕓 34 weeks ago
iOS macOS watchOS tvOS linux macOS iOS
.package(url: "https://github.com/vapor/multipart-kit.git", from: "4.5.4")


Documentation Team Chat MIT License Continuous Integration Swift 5.4 Twitter

🏞 Multipart parser and serializer with Codable support for Multipart Form Data.

Major Releases

The table below shows a list of MultipartKit major releases alongside their compatible NIO and Swift versions.

Version NIO Swift SPM
4.0 2.2 5.4+ from: "4.0.0"
3.0 1.0 4.0+ from: "3.0.0"
2.0 N/A 3.1+ from: "2.0.0"
1.0 N/A 3.1+ from: "1.0.0"

Use the SPM string to easily include the dependency in your Package.swift file.

.package(url: "https://github.com/vapor/multipart-kit.git", from: ...)

Supported Platforms

MultipartKit supports the following platforms:

  • All Linux distributions supported by Swift
  • macOS 10.15+


MultipartKit is a multipart parsing and serializing library. It provides Codable support for the special case of the multipart/form-data media type through a FormDataEncoder and FormDataDecoder. The parser delivers its output as it is parsed through callbacks suitable for streaming.

Multipart Form Data

Let's define a Codable type and a choose a boundary used to separate the multipart parts.

struct User: Codable {
    let name: String
    let email: String
let user = User(name: "Ed", email: "[email protected]")
let boundary = "abc123"

We can encode this instance of a our type using a FormDataEncoder.

let encoded = try FormDataEncoder().encode(foo, boundary: boundary)

The output looks then looks like this.

Content-Disposition: form-data; name="name"

Content-Disposition: form-data; name="email"

[email protected]

In order to decode this message we feed this output and the same boundary to a FormDataDecoder and we get back an identical instance to the one we started with.

let decoded = try FormDataDecoder().decode(User.self, from: encoded, boundary: boundary)

A note on null

As there is no standard defined for how to represent null in Multipart (unlike, for instance, JSON), FormDataEncoder and FormDataDecoder do not support encoding or decoding null respectively.

Nesting and Collections

Nested structures can be represented by naming the parts such that they describe a path using square brackets to denote contained properties or elements in a collection. The following example shows what that looks like in practice.

struct Nested: Encodable {
    let tag: String
    let flag: Bool
    let nested: [Nested]
let boundary = "abc123"
let nested = Nested(tag: "a", flag: true, nested: [Nested(tag: "b", flag: false, nested: [])])
let encoded = try FormDataEncoder().encode(nested, boundary: boundary)

This results in the content below.

Content-Disposition: form-data; name="tag"

Content-Disposition: form-data; name="flag"

Content-Disposition: form-data; name="nested[0][tag]"

Content-Disposition: form-data; name="nested[0][flag]"


Note that the array elements always include the index (as opposed to just []) in order to support complex nesting.


Stars: 124
Last commit: 4 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Fix recursion loop in decoding of a `SingleValueContainer`
34 weeks ago
This patch was authored by @dkolas and released by @0xTim.

Fixes an issue where the Decodable object is expecting a single part, but receives a set of keys causing the FormDataDecoder to get stuck in an infinite loop and eventually crash. This changes the behaviour to throw an error in this case

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics