从 Moya 请求生成 cURL 输出?

Generate cURL output from Moya request?

我正在使用 Moya 并且需要为网络请求打印 cURL。

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 }

我已经查看了 Moya 的文档,但我似乎无法获得想要的结果。我启用了 NetworkLoggingPlugin 但仍然不确定如何为某些请求打印 cURL。谁能帮我找到打印 Moya requestcURL 到控制台的正确方法?

如果您初始化 NetworkLoggerPlugin,它的 cURL 标志默认设置为 false。像 NetworkLoggerPlugin(cURL: true)willSendRequest 一样初始化它应该打印 cURL.

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

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