How do we debug/see the request being set over API with Moya?

For Moya 14.0 and onwards use like this

let plugin: PluginType = NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))

let provider = MoyaProvider<T>(plugins: [plugin])

If using Moya from 14, you can make your own verbose Plugin, like this:

struct VerbosePlugin: PluginType {
    let verbose: Bool

    func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
        #if DEBUG
        if let body = request.httpBody,
           let str = String(data: body, encoding: .utf8) {
            print("request to send: \(str))")
        }
        #endif
        return request
    }
}

and use: let provider = MoyaProvider<AuthService>(plugins: [VerbosePlugin(verbose: true)])


It is done by activating a plugin that Moya Already has it. it is NetworkLoggerPlugin. I need to change this line of code:

var provider = MoyaProvider<MainAPI>()

with:

var provider = MoyaProvider<MainAPI>(plugins: [NetworkLoggerPlugin(verbose: true)])

MOYA >= 14

let plugin: PluginType = NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))

let provider = MoyaProvider<T>(plugins: [plugin])

Thanks to @Anbu.Karthik


Starting from Moya 14.0 you need to do this:

let loggerConfig = NetworkLoggerPlugin.Configuration(logOptions: .verbose)
let networkLogger = NetworkLoggerPlugin(configuration: loggerConfig)    
let provider = MoyaProvider<YourAPI>(plugins: [networkLogger])

Tags:

Ios

Swift

Moya