ResponseDetective is a non-intrusive framework for intercepting any outgoing requests and incoming responses between your app and your server for debugging purposes.
Incorporating ResponseDetective in your project is very simple – it all comes down to a couple of steps.
You may register as many request, response and error interceptors as you like.
// request
InterceptingProtocol.registerRequestInterceptor(BaseInterceptor())
InterceptingProtocol.registerRequestInterceptor(JSONInterceptor())
// response
InterceptingProtocol.registerResponseInterceptor(BaseInterceptor())
InterceptingProtocol.registerResponseInterceptor(JSONInterceptor())
InterceptingProtocol.registerResponseInterceptor(HTMLInterceptor())
// error
InterceptingProtocol.registerErrorInterceptor(BaseInterceptor())
For InterceptingProtocol
to work, it needs to be added as a middleman between your NSURLSession
and the Internet. You can do this by registering it in your session's NSURLSessionConfiguration.protocolClasses
.
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.protocolClasses = [InterceptingProtocol.self]
let session = NSURLSession(configuration: configuration)
If you're using Alamofire as your networking framework, integrating ResponseDetective is as easy as initializing your Manager
with NSURLSessionConfiguration
described above.
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.protocolClasses = [InterceptingProtocol.self]
let manager = Alamofire.Manager(configuration: configuration)
Now it's time to perform the actual request.
let request = NSURLRequest(URL: NSURL(string: "http://httpbin.org/get")!)
let task = session.dataTaskWithRequest(request)
task.resume()
Voilà! Now check out your console output.
200 no error
Content-Length: 301
Server: nginx
Content-Type: application/json
Access-Control-Allow-Origin: *
Date: Wed, 08 Jul 2015 13:10:13 GMT
Access-Control-Allow-Credentials: true
Connection: keep-alive
{
"args" : {
},
"headers" : {
"User-Agent" : "ResponseDetective\/1 CFNetwork\/711.4.6 Darwin\/15.0.0",
"Accept-Encoding" : "gzip, deflate",
"Host" : "httpbin.org",
"Accept-Language" : "en-us",
"Accept" : "*\/*"
},
"origin" : "109.206.223.85",
"url" : "http:\/\/httpbin.org\/get"
}
Interceptor | Description |
---|---|
HeadersInterceptor |
Intercepts all requests and responses and displays their metadata, including errors. |
HTMLInterceptor |
Intercepts only HTML requests and responses. |
ImageInterceptor |
Intercepts only image responses. |
JSONInterceptor |
Intercepts only JSON requests and responses. |
PlainTextInterceptor |
Intercepts only plain text requests and responses. |
XMLInterceptor |
Intercepts only XML requests and responses. |
If you're using Carthage, just add the following dependency to your Cartfile
:
github "netguru/ResponseDetective"
Using ResponseDetective with CocoaPods is as easy as adding the following dependency to your Podfile
:
pod 'ResponseDetective'
Adrian Kashivskyy
Aleksander Popko
ResponseDetective is licensed under the MIT License. See LICENSE.md.