How do we debug the request being set over to backend servers?
I'd like to be able to see exactly or print out the full request with headers parameters, etc... that is being sent over to servers whenever I make any request by Moya
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])
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)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With