-
Notifications
You must be signed in to change notification settings - Fork 100
Home
JohnLui edited this page Nov 4, 2015
·
20 revisions
If you drag Pitaya project into your project, you may need to import it before use it:
import Pitaya
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.
There are alse responseData()
and responseString()
can give us a response.
Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
.responseData({ (data, response) -> Void in
dump(data)
}
Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
.responseString { (string, response) -> Void in
print(string)
}
Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
.onNetworkError({ (error) -> Void in
print("network offline!")
})
.responseJSON { (json, response) -> Void in
print(json["args"]["hello"].stringValue)
}
Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/basic-auth/user/passwd")
.setBasicAuth("user", password: "passwd")
.responseData { (data, response) -> Void in
print(response?.statusCode) // get '200'
}
Pitaya will deal with params in different ways while GET or POST method, just give params to me.
.addParams(["hello": "params"])
let file = File(name: "file", url: NSBundle.mainBundle().URLForResource("Pitaya", withExtension: "png")!)
... ...
.addFiles([file])
.setHTTPHeader(Name: "Accept", Value: "application/json")
let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("lvwenhancom", ofType: "cer")!)!
... ...
.addSSLPinning(LocalCertData: certData) { () -> Void in
print("Under Man-in-the-middle attack!")
}
.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.RAWValue, isJSON: true)
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.RAWValue)
.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)
}