Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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

like image 321
Hamid Shahsavari Avatar asked Jan 09 '19 08:01

Hamid Shahsavari


3 Answers

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

like image 147
Hamid Shahsavari Avatar answered Nov 15 '22 03:11

Hamid Shahsavari


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])
like image 41
Stan Avatar answered Nov 15 '22 04:11

Stan


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)])

like image 32
Eugeny Avatar answered Nov 15 '22 04:11

Eugeny