Warning: This is work in progress
A module for sending HTTP requests in IBM Kitura based applications. It wraps KituraNet ClientRequest class and exposes a familiar interface known from Alamofire.
TODO:
- !! Add URL parameter encoding
- !! Add JSON parameter encoding
- !! Multipart form data parameter encoding
- !! Make tests run on Linux
- !! Write tests to check if resulting ClientRequest is properly initialised
- Add synchronus interface
- Add async interface
- Write instructions
- Switch back to depend on IBM Kitura-net
To install KituraRequest add following line to Dependencies in Package.json:
.Package(url: "https://github.com/IBM-Swift/Kitura-Request.git", majorVersion: 0)Currently Swift 3.0 Release is supported
API of KituraRequest should feel familiar as it closely maps the one of Alamofire.
To create a request object simply call
KituraRequest.request(.get, "https://httpbin.org/get")You can also create a request with parameters by passing [String: Any] dictionary together with an encoding method:
KituraRequest.request(.post,
"https://httpbin.org/post",
parameters: ["foo":"bar"],
encoding: JSONEncoding.default)KituraRequest.request(.post,
"https://httpbin.org/post",
parameters: ["foo":"bar"],
encoding: URLEncoding.default)KituraRequest.request(.post,
"https://httpbin.org/post",
parameters: ["foo":"bar"],
encoding: MultipartEncoding([
BodyPart(key: "file", data: data, mimeType: .image(.png), fileName: "image.jpg")
]))Currently URLEncoding, JSONEncoding and MultipartEncoding encodings is supported by default.
URLEncoding encodes parameters as URLs query.
JSONEncoding converts parameters dictionary to JSON and appends it to request's body.
MultipartEncoding generates multipart http body from passed parameters and appends it to request's body.
When encoding parameters as JSONEncoding or MultipartEncoding appropriate Content-Type header is set.
To create custom parameter encoding extend any class or struct with Encoding protocol.
To set headers in the request pass them as [String: String] dictionary as shown below:
KituraRequest.request(.get,
"https://httpbin.org/get",
headers: ["User-Agent":"Awesome-App"])Currently there is only one method that you can call to get back the requests response and it returns NSData.
KituraRequest.request(.get, "https://google.com").response {
request, response, data, error in
// do something with data
}This library is licensed under Apache 2.0. For details see license.txt