Swiftpack.co - mezhevikin/http-request as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by mezhevikin.
mezhevikin/http-request 0.1.5
🌐 A tiny http client for iOS and macOS. Only 80 lines of code.
⭐️ 28
🕓 1 year ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/mezhevikin/http-request.git", from: "0.1.5")

HttpRequest

A tiny http client for iOS and macOS. Only 80 lines of code.

Get

let request = HttpRequest(
    url: "https://httpbin.org/get",
    parameters: ["name": "Alex"],
)
request.json(HttpBin.self) { json, response in
    print(json)
}

Post

let request = HttpRequest(
    url: "https://httpbin.org/post",
    method: .post,
    parameters: ["name": "Alex"],
    headers: ["User-Agent": "HttpRequest"]
)
request.json(HttpBin.self) { json, response in
    if let json {
         print(json)
    } else if let error = response.error {
        print(error)
    }
}

URLRequest and HTTPURLResponse

var request = HttpRequest(
    url: "https://httpbin.org/get",
)
request.timeoutInterval = 30
request.cachePolicy = .reloadIgnoringCacheData
request.json(HttpBin.self) { json, response in
    print(response.original.statusCode)
    print(response.original.allHeaderFields)
}

Json

struct HttpBin: Codable {
    let args: [String: String]?
    let form: [String: String]?
    let headers: [String: String]?
}

HttpRequest(url: "https://httpbin.org/get").json(HttpBin.self) { json, response in
    print(json)
}

Data and String

HttpRequest(url: "https://httpbin.org/get").data() { data, response in
    if let data {
        let string = String(
            data: data,
            encoding: .utf8
        )
        print(string)
    }
}

Swift Package Manager

https://github.com/mezhevikin/http-request.git

CocoaPods

pod 'HttpRequest', :git => 'https://github.com/mezhevikin/http-request.git'

Links

🌐 HttpRequest for Kotlin/Android

💹 Best Currency Converter

☕️ Buy me a coffee

GitHub

link
Stars: 28
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics