Skip to content
JohnLui edited this page Oct 17, 2015 · 20 revisions

Import

If you drag Pitaya project into your project, you may need to import it before use it:

import Pitaya

Use

Basic Use

Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
    .responseJSON { (json, response) -> Void in
        print(json["args"]["hello"].stringValue)
}

A basic request needs only two method: build() and responseData(), and other modifications are all optional.

Response

There are responseData(), responseJSON() and responseString() can give us a response.

Error callback

.onNetworkError({ (error) -> Void in
    print("network offline!")
})

Basic Auth

.setBasicAuth("user", password: "passwd")

Params

Pitaya will deal with params in different ways while GET or POST method, just give params to me.

.addParams(["hello": "params"])

Files

let file = File(name: "file", url: NSBundle.mainBundle().URLForResource("Pitaya", withExtension: "png")!)
... ...
.addFiles([file])

Custom request HTTP headers

.setHTTPHeader(Name: "Accept", Value: "application/json")

SSL pinning

let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("lvwenhancom", ofType: "cer")!)!
... ...
.addSSLPinning(LocalCertData: certData) { () -> Void in
    print("Under Man-in-the-middle attack!")
}

HTTP Raw Body

.setHTTPBodyRaw("http body")

If you want to set a JSON string to http body in raw:

let json: JSONND = ["user": "JohnLui", "love": "you"]
... ...
.setHTTPBodyRaw(json.jsonStringValue, isJSON: true)

All methods

A request with Params, Files, Basic Auth, SSL pinning, HTTP Raw Body.

// A request with Params, Files, Basic Auth, SSL pinning, HTTP Raw Body and NetworkError callback
let file = File(name: "file", url: NSBundle.mainBundle().URLForResource("Pitaya", withExtension: "png")!)
let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("lvwenhancom", ofType: "cer")!)!
let json: JSONND = ["user": "JohnLui", "love": "you"]
Pita.build(HTTPMethod: .GET, url: "https://lvwenhan.com/")
    .addParams(["hello": "params"])
    .addFiles([file])
    .setHTTPHeader(Name: "Accept", Value: "application/json")
    .setBasicAuth("user", password: "passwd")
    .setHTTPBodyRaw(json.jsonStringValue)
    .onNetworkError({ (error) -> Void in
        print("network offline!")
    })
    .addSSLPinning(LocalCertData: certData) { () -> Void in
        print("Under Man-in-the-middle attack!")
    }
    .responseString { (string, response) -> Void in
        print("HTTP status: " + response!.statusCode.description)
}
Clone this wiki locally