Swift Alamofire How to get the HTTP response status code
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Alamofire provides HTTP response status codes through the response.response?.statusCode property on every request. The status code is an optional Int because the response object is nil when the request fails before reaching the server (no network, DNS failure, timeout). Understanding how to access and handle these codes is essential for building robust networking layers in Swift applications.
Accessing the Status Code
response.response is an HTTPURLResponse?. It is nil when the request never reached the server. The statusCode property is a plain Int — 200, 404, 500, etc.
Handling Different Status Codes
Using Swift's range matching in switch makes it clean to group status codes by category.
Using validate() for Automatic Status Code Checking
Alamofire's validate() method automatically treats non-2xx status codes as errors:
Calling .validate() with no arguments validates that the status code is 200-299 and that the Content-Type matches the Accept header:
Combining Status Code with Response Data
APIs often include error details in the response body even on failure:
Status Code in Async/Await (Alamofire 5.5+)
Or use the simpler .value accessor:
HTTP Status Code Categories
| Range | Category | Meaning |
| 1xx | Informational | Request received, continuing process |
| 2xx | Success | Request successfully received and accepted |
| 3xx | Redirection | Further action needed to complete request |
| 4xx | Client Error | Bad request syntax or cannot be fulfilled |
| 5xx | Server Error | Server failed to fulfill a valid request |
Common Pitfalls
- Force-unwrapping
response.response:response.responseisnilwhen the request fails before reaching the server (no network, DNS failure). Always use optional binding (if let) or provide a default value. - Ignoring
validate()and checking status codes manually everywhere: Alamofire'svalidate()method converts non-2xx responses into errors automatically. Without it, a 404 response with valid JSON still appears as.successin the result. - Assuming status code means the request succeeded: A status code of 200 means the HTTP request succeeded, but the response body may still contain an application-level error. Always check both the status code and the response payload.
- Not handling the
nilresponse case: Whenresponse.responseisnil, there is no status code at all. This happens on network timeouts, airplane mode, or invalid URLs. Treatingnilas 0 or ignoring it causes silent failures. - Using deprecated response serializers: Alamofire 5 replaced
responseJSONcompletion-based APIs withresponseDecodableand async/await. Using the oldresponse.result.valuepattern from Alamofire 4 causes compiler errors.
Summary
- Access status codes via
response.response?.statusCode(returns optionalInt) - Use
validate(statusCode: 200..<300)to automatically treat non-2xx as errors response.responseisnilwhen the request never reached the server- Combine status code checking with response body parsing for complete error handling
- Alamofire 5.5+ supports async/await with
.serializingDecodable()and.value - Always handle both network failures (no response) and HTTP errors (non-2xx status codes)
Related reading
- Swift Alamofire VS AFNetworking
- Swift GET request with parameters
- Swift GET request with parameters
- Swift how to use PREPROCESSOR Flags like if DEBUG to implement API keys?
- Swift alert view with OK and Cancel which button tapped?
- Swift and mutating struct
- Sync data between Android App and webserver
- Synchronization in distributed processes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.