TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Mar 2016 |
SPMSupports SPM | ✓ |
Maintained by Eneko Alonso.
JSONRequest is a tiny Swift library to do HTTP JSON requests.
JSONRequest provides a clean and easy-to-use API to submit HTTP requests both asynchronously and synchronously (see Why Synchronous? Are you crazy?).
let result = JSONRequest.get("http://httpbin.org/get?hello=world")
if let value = result.data?["args"]??["hello"] as? String {
print(value) // Outputs "world"
}
let postResult = JSONRequest.post("http://httpbin.org/post", payload: ["hello": "world"])
if let value = postResult.data?["json"]??["hello"] as? String {
print(value) // Outputs "world"
}
JSONRequest.get("http://httpbin.org/get?hello=world") { result in
if let value = result.data?["args"]??["hello"] as? String {
print(value) // Outputs "world"
}
}
JSONRequest.post("http://httpbin.org/post", payload: ["hello": "world"]) { result in
if let value = result.data?["json"]??["hello"] as? String {
print(value) // Outputs "world"
}
}
URL parameters can be passed as a [String: AnyObject]
dictionary and are automatically URL-encoded and appended to the URL string. All requests allow for query parameters, independently of the HTTP method used. Values of the query parameter dictionary will be converted to String
before being URL-encoded.
Response is automatically parsed (valid JSON response is expected) and returned as AnyObject (valid JSON values include Array
, Dictionary
, Int
, Double
, String
and nil
)
All JSONRequest requests automatically include the following default headers:
Content-Type: application/json
Accept: application/json
The underlining NSMutableURLRequest
object can be accessed via the urlRequest
property.