Latest 0.0.1
Homepage https://github.com/Kuniwak/MultipartFormData
License MIT
Platforms ios 8.0
Frameworks Foundation
Authors

MultipartFormDataKit

Swift 4 compatible
Swift Package Manager and Carthage and CocoaPods compatible
v0.0.5
Build Status

multipart/form-data for Swift.

Basic Usage

let result = MultipartFormData.Builder(
        generatingBoundaryBy: RandomBoundaryGenerator()
    )
    .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)!
        ),
    ])

switch result {
case let .valid(multipartFormData):
    var requrst = URLRequest(url: URL(string: "http://example.com")!)
    request.addValue(multipartFormData.contentType, forHTTPHeaderField: "Content-Type")
    request.httpBody = multipartFormData.body

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

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

Advanced Usage

let multipartFormData = MultipartFormData(
    uniqueAndValidLengthBoundary: "boundary",
    body: [
        MultipartFormData.Part(
            contentDisposition: ContentDisposition(
                name: Name(asPercentEncoded: "field1"),
                filename: nil
            ),
            contentType: nil,
            content: "value1".data(using: .utf8)!
        ),
        MultipartFormData.Part(
            contentDisposition: ContentDisposition(
                name: Name(asPercentEncoded: "field2"),
                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": "MultipartFormData",
    "version": "0.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/MultipartFormData",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Kuniwak": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Kuniwak/MultipartFormData.git",
        "tag": "0.0.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Sources/**/*",
    "frameworks": "Foundation",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This