17

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

Hamish
  • 1,685
  • 22
  • 37

6 Answers6

21

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

Hamish
  • 1,685
  • 22
  • 37
12

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])
Stan
  • 4,169
  • 2
  • 31
  • 39
7

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

Eugeny
  • 308
  • 4
  • 12
6

For Moya 14.0 and onwards use like this

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

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

Cross-posted:

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.

cro
  • 2,437
  • 2
  • 10
  • 9
0
  1. use this to setup the provider let provider: MoyaProvider<API> = MoyaProvider<API>(plugins: [NetworkLoggerPlugin()]
  2. in 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

Pramodya Abeysinghe
  • 1,098
  • 17
  • 13
  • Hi, Is there a way to see my log request and response in the compiler using NetworkLoggerPlugin logOptions? Thank you. (https://stackoverflow.com/questions/61479627/logging-response-and-request-in-moya-14) – Aira Samson Apr 28 '20 at 12:38