【问题标题】:How to log the response for a dart shelf request如何记录飞镖架请求的响应
【发布时间】:2022-01-12 04:47:29
【问题描述】:

我正在使用 Dart Shelf 包,我需要记录它发送的响应。

我已经成功记录了请求,但响应技术不太清楚:

final handler = const shelf.Pipeline()
        .addMiddleware(corsHeaders())
        .addMiddleware(shelf.logRequests(
            logger: (message, isError) =>
                _logRequest(message, isError: isError)))
        .addHandler((req) async {
      final res = await Router().call(req);
      return res;
    });

这个问题分为两部分。

  1. 如何记录标头。
  2. 是否可以记录正文。 我知道有一个问题是响应正文只能读取一次。

由于某些响应可能很大,我需要过滤记录正文的请求。

【问题讨论】:

    标签: dart logging dart-shelf


    【解决方案1】:

    答案有点飞镖。你有一个匿名函数返回一个匿名函数。

    var handler = const Pipeline()
        .addMiddleware(
          (handler) => (request) async {
            final response = await handler(request);
            print(response.headers);
            // you could read the body here, but you'd also need to 
            // save the content and pipe it into a new response instance
            return response;
          },
        )
        .addHandler(syncHandler);
    

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 2013-08-09
      • 1970-01-01
      • 2019-10-04
      相关资源
      最近更新 更多