A barebones Swift HTTP client with automatic JSON response parsing.

Overview

HTTP Client

A barebones Swift HTTP client with automatic JSON response parsing.

Installation

Add HTTP Client as a dependency through Xcode or directly to Package.swift:

.package(url: "https://github.com/joemasilotti/HTTP-Client", branch: "main")

Usage

GET request with no expected success or error response object types.

import HTTP

let client = Client<Empty, Empty>()
let request = Request(url: url)
client.request(request) { result in
    switch result {
    case .success: print("Success!")
    case .failure(let error): print(error.localizedDescription)
    }
}

POST request with both HTTP body and expected success and response object types.

import HTTP

struct Registration: Codable {
    let email: String
    let password: String
}

struct User: Codable {
    let id: Int
    let isAdmin: Bool
}

struct RegistrationError: LocalizedError, Codable, Equatable {
    let status: Int
    let message: String

    var errorDescription: String? { message }
}

let client = Client<User, RegistrationError>()
let registration = Registration(email: "[email protected]", password: "password")
let request = BodyRequest(url: url, method: .post, body: registration)
client.request(request) { result in
    switch result {
    case .success(let response):
        print("HTTP headers", response.headers)
        print("User", response.value)
    case .failure(let error):
        print("Error", error.localizedDescription)
    }
}

HTTP headers can also be set on Request.

import HTTP

let client = Client<Empty, Empty>()
let headers = ["Cookie": "tasty_cookie=strawberry"]
let request = Request(url: url, headers: headers)
client.request(request) { _ in }

URLRequest can be used directly if you require more fine grained control.

import HTTP

let client = Client<Empty, Empty>()
let request = URLRequest(
    url: url,
    cachePolicy: .reloadIgnoringLocalCacheData,
    timeoutInterval: 42.0
)
client.request(request) { _ in }
You might also like...
 Postie - The Next-Level HTTP API Client using Combine
Postie - The Next-Level HTTP API Client using Combine

Postie - The Next-Level HTTP API Client using Combine Postie is a pure Swift library for building URLRequests using property wrappers.

📡 RealHTTP is a lightweight yet powerful client-side HTTP library.
📡 RealHTTP is a lightweight yet powerful client-side HTTP library.

RealHTTP RealHTTP is a lightweight yet powerful client-side HTTP library. Our goal is make an easy to use and effortless http client for Swift. Featur

The HTTP library used by the Spotify iOS client
The HTTP library used by the Spotify iOS client

Authentication and back-off logic is a pain, let's do it once and forget about it! This is a library that allows you to centralise this logic and forg

Cross-platform JsonRPC client implementation with HTTP and WebSocket support

JsonRPC.swift Cross-platform JsonRPC client implementation with HTTP and WebSocket support Getting started Installation Package Manager Add the follow

Twitter-Client - A twitter client that allow users to view tweets on their iphone

Project 3 - Twitter Client Name of your app is a basic twitter app to read your

Lightweight Networking and Parsing framework made for iOS, Mac, WatchOS and tvOS.
Lightweight Networking and Parsing framework made for iOS, Mac, WatchOS and tvOS.

NetworkKit A lightweight iOS, Mac and Watch OS framework that makes networking and parsing super simple. Uses the open-sourced JSONHelper with functio

Elegant HTTP Networking in Swift
Elegant HTTP Networking in Swift

Alamofire is an HTTP networking library written in Swift. Features Component Libraries Requirements Migration Guides Communication Installation Usage

Swift HTTP for Humans
Swift HTTP for Humans

Just is a client-side HTTP library inspired by python-requests - HTTP for Humans. Features Just lets you to the following effortlessly: URL queries cu

Versatile HTTP Networking in Swift
Versatile HTTP Networking in Swift

Net is a versatile HTTP networking library written in Swift. 🌟 Features URL / JSON / Property List Parameter Encoding Upload File / Data / Stream / M

Comments
  • Request and BodyRequest can be initialised with extra headers as required.

    Request and BodyRequest can be initialised with extra headers as required.

    I have added support to pass in headers to Request and BodyRequest which in turn will be set on the underlying URLRequest object. NOTE: BodyRequest will still override any headers it requires like the Content-Type.

    Additionally I have also made the Client be able to make a request directly using a URLRequest object. This is to cover scenarios where the caller needs more properties exposed by URLRequest but still want to leverage all the goodies provided by this Swift package.

    Testing I used TDD to make my changes. Run all unit tests against the Mac and iPhone targets and all tests passed.

    opened by andrejacobs 0
Owner
Joe Masilotti
Independent developer who's passionate about clean, testable code.
Joe Masilotti
Elegantly connect to a JSON api. (Alamofire + Promises + JSON Parsing)

⚠ Important Notice: Farewell ws... hello Networking ! Networking is the next generation of the ws project. Think of it as ws 2.0 built for iOS13. It u

Fresh 351 Oct 2, 2022
Type-safe networking abstraction layer that associates request type with response type.

APIKit APIKit is a type-safe networking abstraction layer that associates request type with response type. // SearchRepositoriesRequest conforms to Re

Yosuke Ishikawa 1.9k Dec 30, 2022
NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. ⚙️🚀

SONetworking NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. Project Folder and File Struc

Ahmad AlSofi 4 Jan 28, 2022
Parsing Simple HTTP Headers for swift

HTTP Headers Parsing simple HTTP headers using pre-defined header descriptions. Examples: let response = HTTPURLRseponse(..., headers: [ "X-RateLi

Alexander Grebenyuk 12 May 17, 2022
Http - Demo for Http Layer

http Example To run the example project, clone the repo, and run pod install fro

null 0 Jan 24, 2022
Swift/Obj-C HTTP framework with a focus on REST and JSON

Now Archived and Forked PMHTTP will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork of PHMT

Postmates Inc. 509 Sep 4, 2022
📱📲 A wrapper for the MultipeerConnectivity framework for automatic offline data transmission between devices

A wrapper for Apple's MultipeerConnectivity framework for offline data transmission between Apple devices. This framework makes it easy to automatical

Wilson Ding 197 Nov 2, 2022
Fully automatic managed circular image array above UIKit.

Automatic Profile cluster view Fully automatic managed circular image array. Usage From Interface Builder • Drag ProfileClusterView.swift file into yo

Chandan Karmakar 5 Dec 9, 2022
A simple GCD based HTTP client and server, written in 'pure' Swift

SwiftyHTTP Note: I'm probably not going to update this any further - If you need a Swift networking toolset for the server side, consider: Macro.swift

Always Right Institute 116 Aug 6, 2022
HTTPClient - HTTP Client With Swift

HTTPClient Ex. Search Repository import HTTPClient let url: URL = .init(string:

zunda 1 Jul 20, 2022