Contents

grdsdev/swift-http-client

A Swift HTTP client built upon [`SwiftOpenAPIRuntime`](https://github.com/apple/swift-openapi-runtime) and [`OpenAPIURLSession`](https://github.com/apple/swift-openapi-urlsession) implementations. This library provides the base types from those libraries, excluding the code gener

Installation

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/grdsdev/swift-http-client", from: "0.1.0")
],
targets: [
    .target(
        name: "YourTarget",
        dependencies: [
            .product(name: "HTTPClient", package: "swift-http-client")
        ]
    )
]

Usage

import HTTPClient

let client = Client(
    serverURL: URL(string: "https://api.example.com")!,
    transport: URLSessionTransport(),
    middlewares: [
        LoggingMiddleware(logger: Logger(label: "HTTPClient")),
        // Add other middlewares here...
    ]
)

let request = HTTPRequest(
    method: .get,
    url: URL(string: "https://api.example.com/users")!,
)

let (response, body) = try await client.send(request)

Multipart Form Data

The library provides built-in support for multipart/form-data requests, making it easy to upload files and form data:

// Using closure builder pattern
let request = HTTPRequest(method: .post, url: URL(string: "/upload")!)

let (response, body) = try await client.send(
    multipartFormData: { formData in
        // Add text fields
        formData.append(
            "John Doe".data(using: .utf8)!,
            withName: "username"
        )
        
        // Add files
        let fileURL = URL(fileURLWithPath: "/path/to/document.pdf")
        formData.append(fileURL, withName: "document")
        
        // Add data with custom filename and MIME type
        let imageData = ... // Your image data
        formData.append(
            imageData,
            withName: "avatar",
            fileName: "profile.jpg",
            mimeType: "image/jpeg"
        )
    },
    with: request
)

The library automatically handles:

  • Setting the correct Content-Type header with boundary
  • Memory-efficient encoding (data < 10 MB is encoded in memory, larger data is streamed from disk)
  • Automatic MIME type detection for file uploads

License

This project is licensed under the MIT License - see the LICENSE file for details.

Package Metadata

Repository: grdsdev/swift-http-client

Default branch: main

README: README.md