Skip to content

๐ŸŒ A tiny http client for iOS and macOS. Only 80 lines of code.

License

Notifications You must be signed in to change notification settings

mezhevikin/http-request

Repository files navigation

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