https://github.com/hlts2/httpswift
Simple http client library written swift
https://github.com/hlts2/httpswift
hlts2 http-client library networking rest simple swift swift-library
Last synced: about 1 month ago
JSON representation
Simple http client library written swift
- Host: GitHub
- URL: https://github.com/hlts2/httpswift
- Owner: hlts2
- License: mit
- Created: 2017-05-26T15:00:38.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-10T14:48:07.000Z (almost 9 years ago)
- Last Synced: 2025-03-13T12:14:25.871Z (over 1 year ago)
- Topics: hlts2, http-client, library, networking, rest, simple, swift, swift-library
- Language: Swift
- Homepage:
- Size: 35.2 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HttpSwift
HttpSwift is simple http client library for swift
## How to use
Fist, you need to import this library
```swift
import HttpSwift
```
## Basic example
### GET
The most basic request.
```swift
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.
```swift
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.
```swift
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.
```swift
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.
```swift
HTTP.Get(url: "https://www.google.co.jp/")
.setAuth(token: "token string")
.do()
}
```
We can also add Basic Authorization.
```swift
HTTP.Get(url: "https://www.google.co.jp/")
.basicAuth(id: "id", pw: "pass word")
.do()
```
We can also add Proxy.
```swift
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.
```swift
let http = HTTP.instance
```
We can also switch handler type
```swift
//no handler
.do()
//with handler
.do({ result in
})
```
## Request type
All the common HTTTP Method is available
- GET
- POST
- PUT
- DELETE
- PATCH
### Request Cancel
You want to cancel the request. You call cancel method.
```swift
let http = HTTP.instance
http.cancel()
```
### Custom Header
You want to add the custom request header.
```swift
let http = HTTP.instance
http.setHeader(headers: ["test1": "1", "test2": "2"])
```
## Requirements
Swift3.0 or latter.
## Installation
HttpSwift is available through [Carthage](https://github.com/Carthage/Carthage) or
[Swift Package Manager](https://github.com/apple/swift-package-manager).
### Carthage
```
github "hlts2/HttpSwift"
```
for detail, please follow the [Carthage Instruction](https://github.com/Carthage/Carthage#if-youre-building-for-ios-tvos-or-watchos)
### Swift Package Manager
```
dependencies: [
.Package(url: "https://github.com/hlts2/HttpSwift.git", majorVersion: 1)
]
```
for detail, please follow the [Swift Package Manager Instruction](https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md)