Skip to content

hlts2/HttpSwift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HttpSwift

HttpSwift is simple http client library for swift

How to use

Fist, you need to import this library

import HttpSwift

Basic example

GET

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

})

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.

let http = HTTP.instance

http.cancel()

Custom Header

You want to add the custom request header.

let http = HTTP.instance

http.setHeader(headers: ["test1": "1", "test2": "2"])

Requirements

Swift3.0 or latter.

Installation

HttpSwift is available through Carthage or Swift Package Manager.

Carthage

github "hlts2/HttpSwift"

for detail, please follow the Carthage Instruction

Swift Package Manager

dependencies: [
    .Package(url: "https://github.com/hlts2/HttpSwift.git", majorVersion: 1)
]

for detail, please follow the Swift Package Manager Instruction

Releases

No releases published

Packages

No packages published

Languages