Search code examples
iosswifthttppostmoya

Get response header with Moya


I am using Moya in my swift app for network requests.

I am able to get requests and map the result using Moya- Object Mapper.

I have used alamofire earlier and I am familiar with how to make post, get requests and read the response headers .

However, I can't seem to understand how I can do the same in Moya.

I did go through the documentation but it does not say anything about reading response headers.

Is there any example or tutorial where I can follow how to do a HTTP authentication and read the response headers. And also on how I can make post requests and send parameters as field parameters or json body.

I have already gone through the Moya example.

Any help will be appreciated. Thank you.


Solution

  • Response header in Moya is in fact quite hard for the moment. It's a unwanted old downcast in Moya code. I never know why they downcasted it.

    I opened a related issue to point it out: Moya header

    And do some change thanks to a pull request: Moya header PR

    This is an example of my personal code where I forcecast the response to get access to the headers:

    class GetTokenRequest {
    
        private let requestService = AuthorizedRequest()
    
        func request() -> Observable<AuthorizedResult<GetTokenEntityResult>> {
            return Observable.deferred { [weak self] in
                guard let wself = self else {
                    return Observable.empty()
                }
    
                let req = wself.requestService.makeRawRequest { userId in
                        let obj = GetTokenEntity(lang: currentLanguage(), userId: userId)
                        return MtxAPI.getToken(obj)
                    }
                    .shareReplay(1)
    
                return req.map { result in
                    switch result {
                    case .success(let response):
                        let urlResponse = response.response as! HTTPURLResponse
                        guard let token = urlResponse.allHeaderFields["Token"] as? String else {
                            return AuthorizedResult.fail(RequestError.technical)
                        }
                        return AuthorizedResult.success(GetTokenEntityResult(token: token))
    
                    case .fail(let error): return AuthorizedResult.fail(error)
                    }
                }
            }
        }
    
    }
    

    I'm using RxSwift but the main line is:

    let urlResponse = response.response as! HTTPURLResponse
    

    You may forcecast it in your map/mapObject/mapArray you use when you get the response (which is a Moya.Response)

    I suggest to you to follow the issue I made, to know when it will be implemented in the next release which will contain this breaking change

    Bonus: if you use SwiftLint in your project, you may cast it with a guard/let

    guard let resp = response.response as? HTTPURLResponse