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
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)])
For Moya 14.0 and onwards use like this
let plugin: PluginType = NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))
let provider = MoyaProvider<T>(plugins: [plugin])
Here's a working example of a verbose plugin that will display both request and response data.
Add the following code to wherever you are calling Moya from:
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) {
if verbose {
print("request to send: \(str))")
}
}
#endif
return request
}
func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) {
#if DEBUG
switch result {
case .success(let body):
if verbose {
print("Response:")
if let json = try? JSONSerialization.jsonObject(with: body.data, options: .mutableContainers) {
print(json)
} else {
let response = String(data: body.data, encoding: .utf8)!
print(response)
}
}
case .failure( _):
break
}
#endif
}
}
In your set up, add the new plugin:
let APIManager = MoyaProvider<API>( plugins: [
VerbosePlugin(verbose: true)
])
This will output both the request being made and the response returned. If the response is JSON encoded, it will pretty-print the JSON, otherwise it will attempt to print out the raw response data.
let provider: MoyaProvider<API> = MoyaProvider<API>(plugins: [NetworkLoggerPlugin()]
NetworkLoggerPlugin.swift
file of Moya pod you can change logOptions
inside the Configuration
struct given. In the init method logOptions: LogOptions = .requestBody
can be changed. It's an enum. So you can try changing the logOptions value as you wish. * I think it's okay to change a pod file because it doesn't affect the code base since this is a developer side logging option