-
Notifications
You must be signed in to change notification settings - Fork 100
Home
zzjzz9266a edited this page Apr 24, 2017
·
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/delay/3", timeout: 10, execution: .sync)
.responseString { (string, response) in
self.resultLabel.text = string
}
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 whether under 'GET' or 'POST' method, just give your params to her.
Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/get?hello=param")
.responseJSON { (json, response) -> Void in
print(json["args"]["hello"].stringValue) // get 'param'
}
let file = File(name: "file", url: NSBundle.mainBundle().URLForResource("Pitaya", withExtension: "png")!)
Pita.build(HTTPMethod: .POST, url: "http://staticonsae.sinaapp.com/pitaya.php")
.addParams(["param": "test"])
.addFiles([file])
.responseString{ (string, response) -> Void in
print(string!) // get '1'
}
let name = "Accept"
let value = "application/json"
Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/headers")
.setHTTPHeader(Name: name, Value: value)
.responseJSON { (json, response) -> Void in
print(json["headers"][name].stringValue) // get 'application/json'
}
let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("lvwenhancom", ofType: "cer")!)!
Pita.build(HTTPMethod: .GET, url: "https://lvwenhan.com/")
.addSSLPinning(LocalCertData: self.certData, SSLValidateErrorCallBack: { () -> Void in
print("Under the Man-in-the-middle attack!")
})
.responseString { (string, response) -> Void in
dump(string)
}
Pita.build(HTTPMethod: .POST, url: "http://httpbin.org/post")
.setHTTPBodyRaw("http body")
.responseJSON { (json, response) -> Void in
print(json["data"].stringValue) // get 'http body'
}
If you want to set a JSON string to http body in raw:
let json: JSONND = ["user": "JohnLui", "love": "you"]
Pita.build(HTTPMethod: .POST, url: "http://httpbin.org/post")
.setHTTPBodyRaw(json.RAWValue, isJSON: true)
.responseJSON { (json, response) -> Void in
print(json["data"].stringValue)
// get:
// {
// "love" : "you",
// "user" : "JohnLui"
// }
print(json["json"]["user"].stringValue) // get 'JohnLui'
print(json["json"]["love"].stringValue) // get 'you'
}
Pitaya
class is an API layer of PitayaManager to make APIs more elegant. If you want to send a request with Pitaya, just do 3 steps:
let pitaya = Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
pitaya.addParams(["hello": "params"])
pitaya.addFiles([file])
pitaya.setHTTPHeader(Name: "Accept", Value: "application/json")
pitaya.setBasicAuth("user", password: "passwd")
pitaya.setHTTPBodyRaw(json.RAWValue)
pitaya.onNetworkError({ (error) -> Void in
print("network offline!")
})
pitaya.addSSLPinning(LocalCertData: certData) { () -> Void in
print("Under Man-in-the-middle attack!")
}
pitaya.responseJSON { (json, response) -> Void in
print(json["args"]["hello"].stringValue)
}
let pita = Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/")
pita.responseString { (string, response) -> Void in
print(string)
}
pita.cancel { () -> Void in
print("Request Cancelled!")
}
You can also get data in NSData or String type by using some other methods.