Requests is a lightweight asynchronous HTTP Requests library written in Swift.
Further HTTP request methods will be implemented at a later date.
import Requests
Requests.get("http://example.com") { response in
print(response.text)
}
Requests.get("http://httpbin.org/ip") { response in
print(response.text)
}
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
...
{
"origin": "127.0.0.1"
}
import Requests
struct IP: Decodable {
var origin: String
}
Requests.get("http://httpbin.org/ip") { response in
let json: IP = response.json()
print(json.origin)
}
127.0.0.1
Requests.get("http://httpbin.org/get") { response in
print(response.text)
}
Requests.post("http://httpbin.org/post") { response in
print(response.text)
}
Requests.post("http://httpbin.org/post", data: ["key": "value"]) { response in
print(response.text)
}
Requests.put("http://httpbin.org/put") { response in
print(response.text)
}
Requests.put("http://httpbin.org/put", data: ["key": "value"]) { response in
print(response.text)
}
Requests.patch("http://httpbin.org/patch") { response in
print(response.text)
}
Requests.patch("http://httpbin.org/patch", data: ["key": "value"]) { response in
print(response.text)
}
Requests.delete("http://httpbin.org/delete") { response in
print(response.text)
}
Requests.delete("http://httpbin.org/delete", data: ["key": "value"]) { response in
print(response.text)
}
let bearerAuthentication = BearerAuthentication(token: "your-token")
Requests.get("https://httpbin.org/bearer", authentication: bearerAuthentication) { response in
print(response.text)
}