Hey
Many http framwork, like okhttp have support for interceptors that allow you to modify the request receive the response. This can be pretty useful for many features like oauth2, content md5 and so on. Is there a way to achieve something similar with alamofire?
Thanks,
Omer
Alamofire already provides APIs that offer complete control over request and response serialization. What feature would the interceptor paradigm offer that isn't already achievable?
I want to achieve the following: Writing an interceptor that add authorization header (I've already saw multiple approach to achieve this with alamofire) and invalidate the token if the request failed with 401. But, I also want that other developers in our company could easily use this and have the full alamofire api (for example, choosing serializer, custom validation etc). With interceptors, they can simply add this to their request...
Given the chainable nature of Alamofire's functionality, you can write those bits of functionality yourself and others can use that code. For instance, you could create a custom request method that adds the header but otherwise just exposes the normal API. A custom validator that contains the behavior you want to delete the token can be chained in as well.
Given that, I thinks there's still some room for improvement.
Yes, but this is a bit cumbersome, I was looking for something that is easier to use. Such an approach also is a bit error prone - more things to remember, meaning probably someone will forget to add them someday...
I second this request. I am trying to write tests that ensure my outgoing requests contain the right body and the right headers. With AFNetworking, I had a simple RequestSerializer in my unit tests that stored the request body on the request object before it was wiped by the framework. Now, that no longer seems possible.
You might be able to solve this with custom encoding, but it will not be pretty...
This request might be solved with the RequestAdapter.
@tobiasoleary is correct. The concept of the "interceptor" is essentially the combination of the RequestAdapter and RequestRetrier protocols added in #1450. There is a fairly detailed example in the README right now that demonstrates how you could build an OAuth2 refresh system using it. This functionality will ship in AF 4.
I'm going to go ahead and close this issue out since those changes cover this request.
Happy refreshing! 馃嵒
the link you provided above doesn't exist anymore
https://github.com/Alamofire/Alamofire/tree/swift3#requestretrier
Most helpful comment
I want to achieve the following: Writing an interceptor that add authorization header (I've already saw multiple approach to achieve this with alamofire) and invalidate the token if the request failed with 401. But, I also want that other developers in our company could easily use this and have the full alamofire api (for example, choosing serializer, custom validation etc). With interceptors, they can simply add this to their request...