Multipart

0.1.2

A simple multipart MIME encoder that supports form-data, files and nesting.
fyrts/Multipart

What's New

0.1.2

2022-04-29T13:41:26Z

What's Changed

  • Add Import FoundationNetwork by @drahot in #2

New Contributors

  • @drahot made their first contribution in #2

Full Changelog: 0.1.1...0.1.2

Multipart

A simple library for creating multipart-encoded message bodies.

Integration

Swift Package Manager

You can use The Swift Package Manager by adding the proper description to your Package.swift file:

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
        .package(url: "https://github.com/Fyrts/Multipart.git", from: "0.1"),
    ]
)

Then run swift build.

Manually

To use this library in your project, drag Multipart.xcodeproj into your workspace.

Usage

Creating simple multipart messages

import Multipart

var message = Multipart(type: .alternative)
message.append(Part(body: "Lorem ipsum dolor sit amet.", contentType: "text/plain"))
message.append(Part(body: "<p><b>Lorem ipsum</b> dolor sit <i>amet</i>.</p>", contentType: "text/html"))
print(message)

Creating advanced multipart messages

import Multipart

// Construct a multipart/alternative message
var plainTextPart = Part(body: "Lorem ipsum dolor sit amet.".data(using: .ascii)!)
plainTextPart.setValue("text/plain", forHeaderField: "Content-Type")
plainTextPart.setAttribute(attribute: "charset", value: "us-ascii", forHeaderField: "Content-Type")

var htmlPart = Part(body: "<p><b>Lorem ipsum</b> dolor sit <i>amet</i>.</p>")
htmlPart.setValue("text/html", forHeaderField: "Content-Type")
htmlPart.setAttribute(attribute: "charset", value: "utf-8", forHeaderField: "Content-Type")

let textParts = Multipart(type: .alternative, parts: [plainTextPart, htmlPart])

// Add a file by wrapping it in a multipart/mixed message
var filePart = Part(body: "Ut enim ad minim veniam, quis nostrud exercitation ullamco.")
filePart.setValue("text/plain", forHeaderField: "Content-Type")
filePart.setAttribute(attribute: "charset", value: "utf-8", forHeaderField: "Content-Type")
filePart.setValue("attachment", forHeaderField: "Content-Disposition")
filePart.setAttribute(attribute: "filename", value: "attachment.txt", forHeaderField: "Content-Disposition")

var mixedMessage = Multipart(type: .mixed, parts: [textParts, filePart])
mixedMessage.preamble = "This is a multi-part message in MIME format."

print(mixedMessage)

Sending form data

Part.FormData is a helper function for quickly building multipart/form-data messages, while URLRequest.setMultipartBody provides simple means of sending the data over HTTP.

import Multipart

var message = Multipart(type: .formData)
message.append(Part.FormData(name: "firstname", value: "Johnny"))
message.append(Part.FormData(name: "lastname", value: "Appleseed"))

var request = URLRequest(url: URL(string: "https://example.com")!)
request.httpMethod = "POST"
request.setMultipartBody(message)

URLSession.shared.dataTask(with: request) { data, response, error in
    print(data, response, error)
}.resume()

Uploading files

Part.FormData also provides basic file upload functionality.

import Multipart

let fileContents = try! Data(contentsOf: URL(string: "/Users/user/Desktop/document.pdf")!)

var message = Multipart(type: .formData)
message.append(Part.FormData(name: "message", value: "See attached file."))
message.append(Part.FormData(name: "file", fileData: fileContents, fileName: "document.pdf", contentType: "application/pdf"))

var request = URLRequest(url: URL(string: "https://example.com")!)
request.httpMethod = "POST"
request.setMultipartBody(message)

URLSession.shared.dataTask(with: request) { data, response, error in
    print(data, response, error)
}.resume()

Sending multipart messages manually

If URLRequest.setMultipartBody does not suit your needs, you can use Multipart.headers and Multipart.body to construct a request yourself.

import Multipart

var message = Multipart(type: .formData)
message.append(Part.FormData(name: "firstname", value: "Johnny"))
message.append(Part.FormData(name: "lastname", value: "Appleseed"))

var request = URLRequest(url: URL(string: "https://example.com")!)
request.httpMethod = "POST"

for header in message.headers {
    request.setValue(header.valueWithAttributes, forHTTPHeaderField: header.name)
}
request.httpBody = message.body

Description

  • Swift Tools 4.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Apr 03 2024 19:12:57 GMT-0900 (Hawaii-Aleutian Daylight Time)