Search code examples
iosswiftmoya

How to modify Moya response before its mapping?


Can I modify the data in Moya response using func process(_ result: Result<Moya.Response, MoyaError>, target: TargetType) -> Result<Moya.Response, MoyaError> method of Moya Plugin? I need to translate the data into a dictionary, add a field, and translate back into the data before the mapping. Thank you!


Solution

  • Yes that would be the preferred way to do it. Another option would be to override the request method in your MoyaProvider subclass and intercept the completion block.

    You need to read and modify your data, and post it back like: let modifiedResponse = Response(statusCode: originalResponse.statusCode, data: modifiedData) return .success(modifiedResponse)