Alamofire Response Validation

In this lesson

You have three choices of varying customizability when validating the responses of your Alamofire requests. You can use some easy, simple automatic validation, you can override that automatic validation, or you can actually help determine the “official” success or failure of the request within the response. Depending on your situation you may use one or both of these for maximum validation! Nothing slips past you!

Kyle Roberts
Swift Guru at Large

Kyle's Series


Tap on time to skip ahead


Hello world, Kyle here with and we’re going to talk about in Alamofire when working with custom HTTP requests a couple ways to validate the responses of those requests. And the first way, which is probably the easiest is that on the request object and before we run any response serialization to call this method validate.


So this method does a few things. It will double check the response’s content type against any declared content types in headers. So I’m not really including any headers here, but we could run it just like that. So this validation method is going to double check the original header that we specified. And if this request did not return data of a JSON type, then the request will be failed. And then that would include an error when we checked the failure of the original request. 


Another thing this does is it checks the associated HTTP status code. I believe the acceptable values are between 2 and 300, and any other status codes, it’ll again, throw an error. And you can run this automatic validation method but there also are a few that are a little more custom. Again, if we do have access to some validation methods that we can specify the accepted content types and status code dynamically instead of Alamofire reading the acceptable header or just viewing the status codes for standard status codes, we can customly, we can manually set those to our own values. 


There’s also an even more custom validation that, I believe that when you create this custom validation or validation subclass, whatever you’re gonna work with, there’s just one validation method that you have to override and that provides you with whatever custom validation you wish. So the other way that is a little more hands on than just running this easy validate method is to actually evaluate the response.result property of our response. And to do that we can run a switch statement on the value of response.result and check for the two available cases: .Success and .Failure. And they each have an associated value. Success’s associated value is actually the property of response.result.value. And the failure’s associated value is any errors that may be attached to this entire request, whether it was from validation or if it’s all the way back from actually running the request. And it’s as easy as that to validate any responses to any Alamofire requests you make.

Additional Info

Register to get access to additional resources and info.