简体   繁体   中英

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
    }
func fetchChats() -> AnyPublisher<DataResponse<ChatListModel, NetworkError>, Never> {
        let url = URL(string: "Your_URL")!
 AF.request(url,
                          method: .get)
            .validate()
            .publishDecodable(type: ChatListModel.self)
            .map { response in
                response.mapError { error in
                    let backendError = response.data.flatMap { try? JSONDecoder().decode(BackendError.self, from: $0)}
                    return NetworkError(initialError: error, backendError: backendError)
                }
            }
            .receive(on: DispatchQueue.main)
            .eraseToAnyPublisher()

Later in viewModel

dataManager.fetchChats()
            .sink { (dataResponse) in
                if dataResponse.error != nil {
                 if error.initialError.responseCode == 401{
                   //do what you like with it 
               }
            } else {
               self.message = dataResponse.value!
            } 
            .store(in: &cancellableSet)

Don't forget to make own Error:

import Foundation
import Alamofire

struct NetworkError: Error {
  let initialError: AFError
  let backendError: BackendError?
}

struct BackendError: Codable, Error {
    var status: String
    var message: String
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM