Minimalistic Swift HTTP request agent for iOS and OS X.
This is a tiny framework that gives you nice a API for crafting HTTP requests.
Throughout this documentation req
is used as an instance of Agent.
The Agent API is simple and easy to use. Simply use Agent.<verb>(url)
and
you're good to go.
It's possible to perform an entire request with a single call. Supply the required parameters when first creating the request. There are usually multiple degrees of overloading.
let done = { (error: NSError?, response: NSHTTPURLResponse?, data: AnyObject?) -> () in
// react to the result of your request
};
Agent.post("http://example.com", headers: [ "Header": "Value" ],
data: [ "Key": "Value" ], done: done)
It's possible to omit most overloaded parameters such as headers
.
Every Agent method returns the Agent itself, therefore it is possible to write more expressive code.
Agent.post("http://example.com")
.send([ "Key": "Value" ])
.end({ (error: NSError?, response: NSHTTPURLResponse?, data: AnyObject?) -> () in
// react to the result of your request
}
)
One of the features that makes Agent is the response closure, instead of setting up a delegate for every HTTP request you have to make. You can simply react to the response in a closure.
In Agent, the response is of the type (NSError?, NSHTTPURLResponse?, AnyObject?)
.
A response closure that reads JSON is easily created as seen below.
let done = { (error: NSError?, response: NSHTTPURLResponse?, data: AnyObject?) -> () in
let json = data! as Dictionary<String, String>
println(json["Key"]!)
}
let req = Agent.get("http://example.com")
req.end({ (error: NSError?, response: NSHTTPURLResponse?, data: AnyObject?) -> () in
// react to the result of your request
})
let req = Agent.post("http://example.com")
req.send([ "Key": "Value" ])
req.end({ (error: NSError?, response: NSHTTPURLResponse?, data: AnyObject?) -> () in
// react to the result of your request
})
let req = Agent.put("http://example.com")
req.send([ "Key": "Value" ])
req.end({ (error: NSError?, response: NSHTTPURLResponse?, data: AnyObject?) -> () in
// react to the result of your request
})
let req = Agent.delete("http://example.com")
req.end({ (error: NSError?, response: NSHTTPURLResponse?, data: AnyObject?) -> () in
// react to the result of your request
})
Will JSON serialize any data
and send it along as the HTTP body. Also
implicitly sets the Content-Type
header to application/json
.
Sets the HTTP header
to value
.
Will start the request and call done
when it's complete.
- If there was an error then
$0
will be anNSErrror
that you can inspect for more information. - If the request was successful then
$1
will be anNSHTTPURLResponse
. - If the response had any data,
$2
will be anAnyObject
that you can type cast to either anArray
orDictionary
.
You can always access the underlying NSMutableURLRequest
using req.request!
, please notice that it is an optional.
We're happy to receive any pull requests. Right now we're working hard on a number of features as seen below.
- Complete asynchronous tests
- Plugins
- Specialized agents (to handle default headers and such)
Any issue is appreciated.
MIT