Swift-multipart-formdata - MultipartFormData: Build multipart/form-data type-safe in Swift

Overview

MultipartFormData

Swift Version License Tweet

Build multipart/form-data type-safe in Swift. A result builder DSL is also available.

Installation

Swift Package Manager

Add the following to the dependencies of your Package.swift:

.package(url: "https://github.com/FelixHerrmann/swift-multipart-formdata.git", from: "x.x.x")

Xcode

Add the package to your project as shown here.

Manual

Download the files in the Sources folder and drag them into you project.

Usage

import MultipartFormData

let boundary = try Boundary(uncheckedBoundary: "example-boundary")
let multipartFormData = try MultipartFormData(boundary: boundary) {
    Subpart {
        ContentDisposition(name: "field1")
    } body: {
        Data("value1".utf8)
    }
    try Subpart {
        ContentDisposition(name: "field2")
        ContentType(mediaType: .applicationJson)
    } body: {
        try JSONSerialization.data(withJSONObject: ["string": "abcd", "int": 1234], options: .prettyPrinted)
    }
    
    let filename = "test.png"
    let homeDirectory = FileManager.default.homeDirectoryForCurrentUser
    let fileDirectory = homeDirectory.appendingPathComponent("Desktop").appendingPathComponent(filename)
    
    if FileManager.default.fileExists(atPath: fileDirectory.path) {
        try Subpart {
            try ContentDisposition(uncheckedName: "field3", uncheckedFilename: filename)
            ContentType(mediaType: .applicationOctetStream)
        } body: {
            try Data(contentsOf: fileDirectory)
        }
    }
}

let url = URL(string: "https://example.com/example")!
let request = URLRequest(url: url, multipartFormData: multipartFormData)
let (data, response) = try await URLSession.shared.data(for: request)
The generated HTTP request
POST https://example.com/example HTTP/1.1
Content-Length: 428
Content-Type: multipart/form-data; boundary="example-boundary"

--example-boundary
Content-Disposition: form-data; name="field1"

value1
--example-boundary
Content-Disposition: form-data; name="field2"
Content-Type: application/json

{
  "string" : "abcd",
  "int" : 1234
}
--example-boundary
Content-Disposition: form-data; name="field3"; filename="test.png"
Content-Type: application/octet-stream

<<png-data>>
--example-boundary--

For a detailed description read the code documentation.

License

MultipartFormData is available under the MIT license. See the LICENSE file for more info.

You might also like...
To practice URLSession to fetch json data from open weather API
To practice URLSession to fetch json data from open weather API

โ›…๏ธ weatherApp-iOS-practice ๐Ÿ“Œ ๊ธฐ๋Šฅ ์ƒ์„ธ ๋„์‹œ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๋ฉด ํ˜„์žฌ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€ ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค

Open source SDK to quickly integrate subscriptions, stop worring about code maintenance, and getting advanced real-time data.  Javascript / iOS glue framework
Open source SDK to quickly integrate subscriptions, stop worring about code maintenance, and getting advanced real-time data. Javascript / iOS glue framework

Open source SDK to quickly integrate subscriptions, stop worring about code maintenance, and getting advanced real-time data. Javascript / iOS glue framework

iOS 15, MVVM, Async Await, Core Data, Abstract Network Layer, Repository & DAO design patterns, SwiftUI and Combine
iOS 15, MVVM, Async Await, Core Data, Abstract Network Layer, Repository & DAO design patterns, SwiftUI and Combine

iOS 15, MVVM, Async Await, Core Data, Abstract Network Layer, Repository & DAO design patterns, SwiftUI and Combine

A library for Search & Parse the weather data from Wunderground & Openweathermap conveniently.
A library for Search & Parse the weather data from Wunderground & Openweathermap conveniently.

SmileWeather A library for Search & Parse the weather data from Wunderground & Openweathermap conveniently. #What can it do for you? 1. Handle all com

Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and moreโ€ฆ)
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and moreโ€ฆ)

Perfect: Server-Side Swift ็ฎ€ไฝ“ไธญๆ–‡ Perfect: Server-Side Swift Perfect is a complete and powerful toolbox, framework, and application server for Linux, iO

Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux.

BlueSocket Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux. Prerequisites Swift Swift Open Source swift-5.1

Swift Express is a simple, yet unopinionated web application server written in Swift
Swift Express is a simple, yet unopinionated web application server written in Swift

Documentation h5 align="right"a href="http://demo.swiftexpress.io/"Live ๐Ÿง server running Demo img src="https://cdn0.iconfinder.com/data/icons/

Swift backend / server framework (Pure Swift, Supports Linux)

NetworkObjects NetworkObjects is a #PureSwift backend. This framework compiles for OS X, iOS and Linux and serves as the foundation for building power

Quickstart-ios-swift-grpc - Quickstart for integrating Approov with iOS apps in Swift that make API requests you wish to protect using GRPC
Comments
  • Enable documentation build action workflow

    Enable documentation build action workflow

    The workflow is already in master, just disabled right now. The documentation build process relies on swift-docc-plugin which requires Swift 5.6 and therefore Xcode 13.3; GitHub-Actions don't have an Xcode 13.3 runner atm...

    documentation 
    opened by FelixHerrmann 1
  • Add missing CRLF epilogue

    Add missing CRLF epilogue

    Missing the extra CRLF that marks the end of the outer headers.

    multipart-body := [preamble CRLF]
                      dash-boundary CRLF
                      body-part *encapsulation
                      close-delimiter
                      [CRLF epilogue]
    

    See: https://datatracker.ietf.org/doc/html/rfc2046#page-22

    If the server is using a strict parser, the request will fail. (such as Google)

    opened by MMP0 1
Releases(1.0.1)
  • 1.0.1(Feb 1, 2022)

    What's Changed

    • Add missing CRLF epilogue by @MMP0 in https://github.com/FelixHerrmann/swift-multipart-formdata/pull/2

    New Contributors

    • @MMP0 made their first contribution in https://github.com/FelixHerrmann/swift-multipart-formdata/pull/2

    Full Changelog: https://github.com/FelixHerrmann/swift-multipart-formdata/compare/1.0.0...1.0.1

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Dec 30, 2021)

Owner
Felix Herrmann
iOS @scoutapp-ai
Felix Herrmann
A phantom type is a custom type that has one or more unused type parameters.

PhantomTypes A phantom type is a custom type that has one or more unused type parameters. Phantom types allow you to enforce type-safety without sacri

null 3 Nov 4, 2022
A type-safe, high-level networking solution for Swift apps

What Type-safe network calls made easy Netswift offers an easy way to perform network calls in a structured and type-safe way. Why Networking in Swift

Dorian Grolaux 23 Apr 27, 2022
A type-safe packet processor framework in Swift

PacketProcessor The Swift PacketProcessor provides a simple, type-safe way of handling structured packets given a data stream. PacketProcessor handles

Danny Sung 12 Oct 9, 2022
MQTTNIO - Non-blocking, event-driven Swift client for MQTT build on SwiftNIO

This library has support for WebSocket connections and TLS. It runs on all platforms Swift NIO runs on (e.g. macOS, iOS, Linux, etc.).

Steven Roebert 41 Dec 23, 2022
A tool to build projects on MacOS and a remote linux server with one command

DualBuild DualBuild is a command line tool for building projects on MacOS and a remote Linux server. ##Setup Install the repository git clone https://

Operator Foundation 0 Dec 26, 2021
Commonly available global error type packages

ErrorKit ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ์ „์—ญ ์˜ค๋ฅ˜ ํƒ€์ž… ํŒจํ‚ค์ง€. Requirements Xcode 13.2.1+ Swift 5.5+ Ins

Jaemyeong Jin 0 Nov 6, 2022
Swift Paging is a framework that helps you load and display pages of data from a larger dataset from local storage or over network.

Swift Paging is a framework that helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently. It's built on top of Combine, allowing you to harness its full power, handle errors easily, etc.

Gordan Glavaลก 12 Dec 9, 2022
ServiceData is an HTTP networking library written in Swift which can download different types of data.

ServiceData Package Description : ServiceData is an HTTP networking library written in Swift which can download different types of data. Features List

Mubarak Alseif 0 Nov 11, 2021
An Alamofire extension which converts JSON response data into swift objects using EVReflection

AlamofireJsonToObjects ?? This is now a subspec of EVReflection and the code is maintained there. ?? You can install it as a subspec like this: use_fr

Edwin Vermeer 161 Sep 29, 2022
๐Ÿ“ฑ๐Ÿ“ฒ A wrapper for the MultipeerConnectivity framework for automatic offline data transmission between devices

A wrapper for Apple's MultipeerConnectivity framework for offline data transmission between Apple devices. This framework makes it easy to automatical

Wilson Ding 197 Nov 2, 2022