HttpSwift is simple http client library for swift
Fist, you need to import this library
import HttpSwift
The most basic request.
HTTP.Get(url: "https://www.google.co.jp/")
.do() { result in
switch result {
case .success(let value):
print(value)
case .failure(let error):
print(error)
}
}
We can also add parameters.
HTTP.Get(url: "https://www.google.co.jp/")
.setQuery(params: ["a": "1", "b": "2"])
.do() { result in
switch result {
case .success(let value):
print(value)
case .failure(let error):
print(error)
}
}
We can also add content-type. returned object is json selialized object.
HTTP.Get(url: "https://www.google.co.jp/")
.setQuery(params: ["a": "1", "b": "2"])
.setContentsType(.json)
.do() { result in
switch result {
case .success(let value):
print(value) //Json Serialized Object
case .failure(let error):
print(error)
}
}
We can also add User-Agent.
HTTP.Get(url: "https://www.google.co.jp/")
.setUserAgent(agent: "Agent_Name")
.do() { result in
switch result {
case .success(let value):
print(value)
case .failure(let error):
print(error)
}
}
We can also add Authorization.
HTTP.Get(url: "https://www.google.co.jp/")
.setAuth(token: "token string")
.do()
}
We can also add Basic Authorization.
HTTP.Get(url: "https://www.google.co.jp/")
.basicAuth(id: "id", pw: "pass word")
.do()
We can also add Proxy.
HTTP.Get(url: "https://www.google.co.jp/")
.setProxy(host: "192.168.3.30", port: "3030")
.do()
when reusing request. HTTP instance is singleton object.
let http = HTTP.instance
We can also switch handler type
//no handler
.do()
//with handler
.do({ result in
})
All the common HTTTP Method is available
-
GET
-
POST
-
PUT
-
DELETE
-
PATCH
You want to cancel the request. You call cancel method.
let http = HTTP.instance
http.cancel()
You want to add the custom request header.
let http = HTTP.instance
http.setHeader(headers: ["test1": "1", "test2": "2"])
Swift3.0 or latter.
HttpSwift is available through Carthage or Swift Package Manager.
github "hlts2/HttpSwift"
for detail, please follow the Carthage Instruction
dependencies: [
.Package(url: "https://github.com/hlts2/HttpSwift.git", majorVersion: 1)
]
for detail, please follow the Swift Package Manager Instruction