-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2820284
commit 8652854
Showing
1 changed file
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,103 @@ | ||
# swift-multipart-formdata | ||
# MultipartFormData | ||
|
||
A description of this package. | ||
[![](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) | ||
[![](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) | ||
[![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) | ||
[![Version](https://img.shields.io/github/v/release/FelixHerrmann/swift-multipart-formdata)](https://github.com/FelixHerrmann/swift-multipart-formdata/releases) | ||
[![License](https://img.shields.io/github/license/FelixHerrmann/swift-multipart-formdata)](https://github.com/FelixHerrmann/swift-multipart-formdata/blob/master/LICENSE) | ||
[![Tweet](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2FFelixHerrmann%2Fswift-multipart-formdata)](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2FFelixHerrmann%2Fswift-multipart-formdata) | ||
|
||
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. | ||
|
||
|
||
## Usage | ||
|
||
```swift | ||
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) | ||
``` | ||
|
||
<details> | ||
<summary>The generated HTTP request</summary> | ||
|
||
```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 | ||
<<png-data>> | ||
--example-boundary-- | ||
``` | ||
</details> | ||
|
||
> For a detailed description read the code documentation. | ||
## License | ||
|
||
MultipartFormData is available under the MIT license. See the [LICENSE](/LICENSE) file for more info. |