HTTP

0.4.0

The HTTP package provides a Swift interface for making HTTP requests and processing responses.
grdsdev/swift-http

What's New

0.4.0

2025-04-04T13:40:25Z

Changes

  • Rename from Fetch to HTTP

Full Changelog: 0.3.0...0.4.0

HTTP

codecov

The HTTP package provides a Swift interface for making HTTP requests and processing responses. It is designed to be simple and intuitive, allowing developers to easily integrate HTTP functionality into their Swift applications.

Features

  • Simple and intuitive API for making HTTP requests
  • Support for various HTTP methods (GET, POST, PUT, DELETE, etc.)
  • Flexible request options, including custom headers and body
  • Asynchronous operations using Swift's async/await
  • Built-in support for JSON encoding and decoding
  • Multipart form data support
  • URL search parameters handling
  • Support for streamed responses

Installation

To integrate the HTTP package into your Swift project, add the following line to your Package.swift file:

.package(url: "https://github.com/grdsdev/swift-http.git", from: "0.0.1")

Then, include "HTTP" in your target dependencies:

.target(
  name: "YourTargetName", 
  dependencies: [
    .product(name: "HTTP", package: "swift-http"), 
    .product(name: "HTTPFoundation", package: "swift-http"),
  ]
)

Usage

Basic GET Request

import HTTP
import HTTPFoundation

let response = try await http.get("https://api.example.com/data")
let json = try await response.json()

POST Request with JSON Body

import HTTP
import HTTPFoundation

let response = try await http.post(
  "https://api.example.com/users", 
  body: ["name": "John Doe", "age": 30]
)

Multipart Form Data

import HTTP
import HTTPFoundation

var formData = FormData()
formData.append("username", "johndoe")
formData.append("avatar", imageData, filename: "avatar.jpg", contentType: "image/jpeg")

let response = try await http.post(
  "https://api.example.com/upload", 
  body: formData
)

URL Search Parameters

import HTTP

var params = URLSearchParams("https://example.com?foo=1&bar=2")
params.append("baz", "3")
print(params.description) // Output: foo=1&bar=2&baz=3

Streamed Responses

import HTTP
import HTTPFoundation

let response = try await http.get("https://api.example.com/stream")

for await chunk in response.body {
  // handle chunk of Data
}

API Reference

For detailed API documentation, please refer to the inline comments in the source code.

Requirements

  • Swift 5.9+
  • macOS 10.15+ or iOS 13.0+

License

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

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

Last updated: Wed May 14 2025 13:19:43 GMT-0900 (Hawaii-Aleutian Daylight Time)