HttpRequest

一个适用于 iOS 和 macOS 的微型 http 客户端。只有 80 行 代码。

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 和 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(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 和 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(Swift 包管理器)

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

CocoaPods

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

链接

🌐 HttpRequest for Kotlin/Android(Kotlin/Android 版本的 HttpRequest)

💹 Best Currency Converter(最佳货币转换器)

☕️ Buy me a coffee(请我喝杯咖啡)