2
votes

I'm using Moya and need to print the cURL for a network request.

Usually, in Alamofire 4, I would do something like this:

let req = Alamofire.request(someURLRequestConvertible)
debugPrint(req) // will print cURL

My call site for Moya looks like this:

MyMoyaProvider.request(MyEndPoints.login(params)) { (result) in }

I've checked out the documentation for Moya, but I can't seem to get the results I'm looking for. I enabled the NetworkLoggingPlugin but still unsure how to print cURL for certain requests. Can someone help me find the proper way to print the Moya request's cURL to console?

2

2 Answers

5
votes

If you initialize your NetworkLoggerPlugin, its cURL flag is set to false by default. Initializing it like NetworkLoggerPlugin(cURL: true), willSendRequest should print the cURL.

As per @BasThomas on GitHub: https://github.com/Moya/Moya/issues/1037#event-1027530791

1
votes

For Moya 14.0.*

static fileprivate let provider = MoyaProvider<ApiService>(endpointClosure: { (target: ApiService) -> Endpoint in
    let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
    switch target {
    default:
        let httpHeaderFields = ["Content-Type" : "application/json"]
        return defaultEndpoint.adding(newHTTPHeaderFields: httpHeaderFields)
    }
}, plugins: [
    NetworkLoggerPlugin(configuration: .init(formatter: .init(), output: { (target, array) in
        if let log = array.first {
            print(log)
        }
    }, logOptions: .formatRequestAscURL))
])