{"id":13872344,"url":"https://github.com/fyrts/Multipart","last_synced_at":"2025-07-16T02:30:41.781Z","repository":{"id":42453384,"uuid":"134428800","full_name":"fyrts/Multipart","owner":"fyrts","description":"A simple multipart MIME encoder that supports form-data, files and nesting.","archived":false,"fork":false,"pushed_at":"2022-04-29T13:39:29.000Z","size":17,"stargazers_count":11,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-30T17:14:35.311Z","etag":null,"topics":["mime","multipart","multipart-formdata","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fyrts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-22T14:36:50.000Z","updated_at":"2024-06-20T18:39:58.000Z","dependencies_parsed_at":"2022-08-31T22:50:52.703Z","dependency_job_id":null,"html_url":"https://github.com/fyrts/Multipart","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fyrts%2FMultipart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fyrts%2FMultipart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fyrts%2FMultipart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fyrts%2FMultipart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fyrts","download_url":"https://codeload.github.com/fyrts/Multipart/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226095684,"owners_count":17572970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mime","multipart","multipart-formdata","swift"],"created_at":"2024-08-05T23:00:40.463Z","updated_at":"2024-11-23T20:30:55.302Z","avatar_url":"https://github.com/fyrts.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# Multipart\n\nA simple library for creating multipart-encoded message bodies.\n\n## Integration\n\n#### Swift Package Manager\n\nYou can use [The Swift Package Manager](https://swift.org/package-manager) by adding the proper description to your `Package.swift` file:\n\n```swift\n// swift-tools-version:4.0\nimport PackageDescription\n\nlet package = Package(\n    name: \"YOUR_PROJECT_NAME\",\n    dependencies: [\n        .package(url: \"https://github.com/Fyrts/Multipart.git\", from: \"0.1\"),\n    ]\n)\n```\nThen run `swift build`.\n\n#### Manually\n\nTo use this library in your project, drag Multipart.xcodeproj into your workspace.\n\n## Usage\n\n#### Creating simple multipart messages\n\n```swift\nimport Multipart\n\nvar message = Multipart(type: .alternative)\nmessage.append(Part(body: \"Lorem ipsum dolor sit amet.\", contentType: \"text/plain\"))\nmessage.append(Part(body: \"\u003cp\u003e\u003cb\u003eLorem ipsum\u003c/b\u003e dolor sit \u003ci\u003eamet\u003c/i\u003e.\u003c/p\u003e\", contentType: \"text/html\"))\nprint(message)\n```\n\n#### Creating advanced multipart messages\n\n```swift\nimport Multipart\n\n// Construct a multipart/alternative message\nvar plainTextPart = Part(body: \"Lorem ipsum dolor sit amet.\".data(using: .ascii)!)\nplainTextPart.setValue(\"text/plain\", forHeaderField: \"Content-Type\")\nplainTextPart.setAttribute(attribute: \"charset\", value: \"us-ascii\", forHeaderField: \"Content-Type\")\n\nvar htmlPart = Part(body: \"\u003cp\u003e\u003cb\u003eLorem ipsum\u003c/b\u003e dolor sit \u003ci\u003eamet\u003c/i\u003e.\u003c/p\u003e\")\nhtmlPart.setValue(\"text/html\", forHeaderField: \"Content-Type\")\nhtmlPart.setAttribute(attribute: \"charset\", value: \"utf-8\", forHeaderField: \"Content-Type\")\n\nlet textParts = Multipart(type: .alternative, parts: [plainTextPart, htmlPart])\n\n// Add a file by wrapping it in a multipart/mixed message\nvar filePart = Part(body: \"Ut enim ad minim veniam, quis nostrud exercitation ullamco.\")\nfilePart.setValue(\"text/plain\", forHeaderField: \"Content-Type\")\nfilePart.setAttribute(attribute: \"charset\", value: \"utf-8\", forHeaderField: \"Content-Type\")\nfilePart.setValue(\"attachment\", forHeaderField: \"Content-Disposition\")\nfilePart.setAttribute(attribute: \"filename\", value: \"attachment.txt\", forHeaderField: \"Content-Disposition\")\n\nvar mixedMessage = Multipart(type: .mixed, parts: [textParts, filePart])\nmixedMessage.preamble = \"This is a multi-part message in MIME format.\"\n\nprint(mixedMessage)\n```\n\n#### Sending form data\n\n`Part.FormData` is a helper function for quickly building `multipart/form-data` messages, while `URLRequest.setMultipartBody`\nprovides simple means of sending the data over HTTP. \n\n```swift\nimport Multipart\n\nvar message = Multipart(type: .formData)\nmessage.append(Part.FormData(name: \"firstname\", value: \"Johnny\"))\nmessage.append(Part.FormData(name: \"lastname\", value: \"Appleseed\"))\n\nvar request = URLRequest(url: URL(string: \"https://example.com\")!)\nrequest.httpMethod = \"POST\"\nrequest.setMultipartBody(message)\n\nURLSession.shared.dataTask(with: request) { data, response, error in\n    print(data, response, error)\n}.resume()\n```\n\n#### Uploading files\n\n`Part.FormData` also provides basic file upload functionality.\n\n```swift\nimport Multipart\n\nlet fileContents = try! Data(contentsOf: URL(string: \"/Users/user/Desktop/document.pdf\")!)\n\nvar message = Multipart(type: .formData)\nmessage.append(Part.FormData(name: \"message\", value: \"See attached file.\"))\nmessage.append(Part.FormData(name: \"file\", fileData: fileContents, fileName: \"document.pdf\", contentType: \"application/pdf\"))\n\nvar request = URLRequest(url: URL(string: \"https://example.com\")!)\nrequest.httpMethod = \"POST\"\nrequest.setMultipartBody(message)\n\nURLSession.shared.dataTask(with: request) { data, response, error in\n    print(data, response, error)\n}.resume()\n```\n\n#### Sending multipart messages manually\n\nIf `URLRequest.setMultipartBody` does not suit your needs, you can use `Multipart.headers` and `Multipart.body` to construct a\nrequest yourself.\n\n```swift\nimport Multipart\n\nvar message = Multipart(type: .formData)\nmessage.append(Part.FormData(name: \"firstname\", value: \"Johnny\"))\nmessage.append(Part.FormData(name: \"lastname\", value: \"Appleseed\"))\n\nvar request = URLRequest(url: URL(string: \"https://example.com\")!)\nrequest.httpMethod = \"POST\"\n\nfor header in message.headers {\n    request.setValue(header.valueWithAttributes, forHTTPHeaderField: header.name)\n}\nrequest.httpBody = message.body\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffyrts%2FMultipart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffyrts%2FMultipart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffyrts%2FMultipart/lists"}