Alamofire + Combine: Get the HTTP response status code

I am currently using Alamofire which contains Combine support and using it following way:

    let request = AF.request(endpoint)

    ...
    request
            .publishDecodable(type: T.self, decoder: decoder)
            .value()
            .eraseToAnyPublisher()

This will publish result and AFError but from subscriber's .sink, I can't find anywhere to get the HTTP status code. What's the best way to get the status code in subscriber?


If you want the response code, don't erase the DataPublisher using .value(). Instead, use the DataResponse you get from the various publish methods, which includes all of the various response information, including status code. You can then .map it into whatever type you need.


For Swift 5.X and Xcode 12.4 For debugging purposes you can intercept the response right before the Combine publisher (publishDecodable()) and get some of the elements of the URL Response, with :

session.request(signedRequest)
    .responseJSON { response in
        print(response.request)  // original URL request
        print(response.response) // URL response
        print(response.data)     // server data
        print(response.result)   // result of response serialization
    }