Latest 1.0.1
Homepage https://github.com/Kuniwak/MultipartFormDataKit
License MIT
Platforms ios 9.0
Frameworks Foundation
Authors

Swift 4 compatible
Swift Package Manager and Carthage and CocoaPods compatible
v1.0.1
Build Status

multipart/form-data for Swift.

Basic Usage

let multipartFormData = try MultipartFormData.Builder.build(
    with: [
        (
            name: "example1",
            filename: nil,
            mimeType: nil,
            data: "Hello, World!".data(using: .utf8)!
        ),
        (
            name: "example2",
            filename: "example.txt",
            mimeType: MIMEType.textPlain,
            data: "EXAMPLE_TXT".data(using: .utf8)!
        ),
    ],
    willSeparateBy: RandomBoundaryGenerator.generate()
)

var request = URLRequest(url: URL(string: "http://example.com")!)
request.httpMethod = "POST"
request.setValue(multipartFormData.contentType, forHTTPHeaderField: "Content-Type")
request.httpBody = multipartFormData.body

let task = URLSession.shared.dataTask(with: request)
task.resume()

Advanced Usage

let multipartFormData = MultipartFormData(
    uniqueAndValidLengthBoundary: "boundary",
    body: [
        MultipartFormData.Part(
            contentDisposition: ContentDisposition(
                name: Name(asPercentEncoded: "field%201"),
                filename: nil
            ),
            contentType: nil,
            content: "value1".data(using: .utf8)!
        ),
        MultipartFormData.Part(
            contentDisposition: ContentDisposition(
                name: Name(asPercentEncoded: "field%202"),
                filename: Filename(asPercentEncoded: "example.txt")
            ),
            contentType: ContentType(representing: .textPlain),
            content: "value2".data(using: .utf8)!
        ),
    ]
)

print(multipartFormData.header.name)
// Content-Type

print(multipartFormData.header.value)
// multipart/form-data; boundary="boundary"

switch multipartFormData.asData() {
case let .valid(data):
    print(String(data: data, encoding: .utf8))

    // --boundary
    // Content-Disposition: form-data; name="field1"
    // 
    // value1
    // --boundary
    // Content-Disposition: form-data; name="filed2"; filename="example.txt"
    // Content-Type: text/plain
    // 
    // value2
    // --boundary--

case let .invalid(error):
    print(error)
}

License

MIT

Latest podspec

{
    "name": "MultipartFormDataKit",
    "version": "1.0.1",
    "summary": "multipart/form-data for Swift.",
    "description": "multipart/form-data for Swift.nYou can communicate by using this module and URLSession or other HTTP libraries.",
    "homepage": "https://github.com/Kuniwak/MultipartFormDataKit",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Kuniwak": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Kuniwak/MultipartFormDataKit.git",
        "tag": "1.0.1"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "Sources/**/*",
    "frameworks": "Foundation",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This