Giter Club home page Giter Club logo

swift-multipart-formdata's Introduction

MultipartFormData

Swift SwiftLint

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

Installation

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.

Example

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--

Documentation

For a detailed usage description, you can check out the documentation.

docs docs

License

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

swift-multipart-formdata's People

Contributors

dependabot[bot] avatar felixherrmann avatar mmp0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

swift-multipart-formdata's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.