Skip to content

Repository files navigation

swift-http-client

A Swift HTTP client built upon SwiftOpenAPIRuntime and OpenAPIURLSession implementations. This library provides the base types from those libraries, excluding the code generation specific code.

If you want to leverage the benefits of those Apple libraries without using code generation, this is the library for you.

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.

About

A Swift HTTP client built upon SwiftOpenAPIRuntime and OpenAPIURLSession implementations.

Resources

Stars

Watchers

Forks

Releases

Used by

Contributors

Languages