Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/FelixHerrmann/swift-multipart-formdata
Build multipart/form-data type-safe in Swift.
https://github.com/FelixHerrmann/swift-multipart-formdata
dsl multipart-formdata resultbuilder swift swiftpackage
Last synced: about 2 months ago
JSON representation
Build multipart/form-data type-safe in Swift.
- Host: GitHub
- URL: https://github.com/FelixHerrmann/swift-multipart-formdata
- Owner: FelixHerrmann
- License: mit
- Created: 2021-12-26T22:55:25.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-03-03T21:37:26.000Z (10 months ago)
- Last Synced: 2024-07-13T09:33:11.077Z (5 months ago)
- Topics: dsl, multipart-formdata, resultbuilder, swift, swiftpackage
- Language: Swift
- Homepage:
- Size: 95.7 KB
- Stars: 30
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - swift-multipart-formdata - data type-safe in Swift. A result builder DSL is also available. (主要参考用result builder封装multipart-formdata数据) <br> (Network API)
README
# MultipartFormData
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FFelixHerrmann%2Fswift-multipart-formdata%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/FelixHerrmann/swift-multipart-formdata)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FFelixHerrmann%2Fswift-multipart-formdata%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/FelixHerrmann/swift-multipart-formdata)
[![Swift](https://github.com/FelixHerrmann/swift-multipart-formdata/actions/workflows/swift.yml/badge.svg)](https://github.com/FelixHerrmann/swift-multipart-formdata/actions/workflows/swift.yml)
[![SwiftLint](https://github.com/FelixHerrmann/swift-multipart-formdata/actions/workflows/swiftlint.yml/badge.svg)](https://github.com/FelixHerrmann/swift-multipart-formdata/actions/workflows/swiftlint.yml)Build multipart/form-data type-safe in Swift. A result builder DSL is also available.
## Installation
### [Swift Package Manager](https://swift.org/package-manager/)
Add the following to the dependencies of your `Package.swift`:
```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](https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app).
### Manual
Download the files in the [Sources](/Sources) folder and drag them into you project.
## Example
```swift
import MultipartFormDatalet 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
```http
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<>
--example-boundary--
```## Documentation
For a detailed usage description, you can check out the [documentation](https://swiftpackageindex.com/FelixHerrmann/swift-multipart-formdata/master/documentation/multipartformdata).
![docs](https://user-images.githubusercontent.com/42500484/193477691-ff5fa9a7-8a3e-48bd-aade-a853144ab05f.png#gh-light-mode-only)
![docs](https://user-images.githubusercontent.com/42500484/193477695-cebe2417-6311-4ef0-bbe3-2e5217a89ab9.png#gh-dark-mode-only)## License
MultipartFormData is available under the MIT license. See the [LICENSE](/LICENSE) file for more info.