GithubHelp home page GithubHelp logo

christopherweems / multipart-kit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andrew804/multipart-kit

0.0 0.0 0.0 478 KB

๐Ÿž Parses and serializes multipart-encoded data with Codable support.

License: MIT License

Swift 100.00%

multipart-kit's Introduction

MultipartKit

Documentation Team Chat MIT License Continuous Integration Swift 5.7+

๐Ÿž Multipart parser and serializer with Codable support for Multipart Form Data.

Installation

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.

Add MultiPartKit to your package dependencies:

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

Add MultiPartKit to your target's dependencies:

targets: [
    .target(name: "MyAppTarget", dependencies: [
        // ...
        .product(name: "MultipartKit", package: "multipart-kit"),
    ])
]

Supported Platforms

MultipartKit supports the following platforms:

  • All Linux distributions supported by Swift 5.7+
  • macOS 10.15+

Overview

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.

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

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

[email protected]
--abc123--

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.

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

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

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

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

false
--abc123--

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

multipart-kit's People

Contributors

tanner0101 avatar siemensikkema avatar andrew804 avatar 0xtim avatar loganwright avatar ole avatar gwynne avatar jaapwijnen avatar vzsg avatar t-ae avatar vi4m avatar bre7 avatar brettrtoomey avatar bennydebock avatar ulrikdamm avatar adam-fowler avatar ahmdyasser avatar calebkleveter avatar ckd avatar dkolas avatar mihaelisaev avatar patrick-kladek avatar grundoon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.