buildReplayRequest function

Request buildReplayRequest(
  1. NetworkRequest r, {
  2. String? url,
  3. Map<String, String>? headers,
  4. Object? body,
})

根据已捕获的 NetworkRequest 重建一个可发送的 http.Request, 用于「重放 / 重试」功能(在 App 内重新发出同一请求)。 Rebuild a sendable http.Request from a captured NetworkRequest for replay/retry.

可选覆盖 / Optional overrides(用于「编辑后重放」):

  • url:替换目标 URL / replace the target URL
  • headers:合并进请求头(同键覆盖)/ merged into the headers (same key wins)
  • body:替换请求体;传 null 表示沿用原 body, 传空字符串表示清空 body / replace the body; null keeps the original, an empty string clears it

Implementation

http.Request buildReplayRequest(
  NetworkRequest r, {
  String? url,
  Map<String, String>? headers,
  Object? body,
}) {
  final target = Uri.parse(url ?? r.url);
  final req = http.Request(r.method.toUpperCase(), target);

  if (r.headers != null) {
    for (final entry in r.headers!.entries) {
      if (replayExcludedHeaders.contains(entry.key.toLowerCase())) continue;
      req.headers[entry.key] = entry.value;
    }
  }
  if (headers != null) {
    for (final entry in headers.entries) {
      if (replayExcludedHeaders.contains(entry.key.toLowerCase())) continue;
      req.headers[entry.key] = entry.value;
    }
  }

  final effectiveBody = body ?? r.body;
  if (effectiveBody != null) {
    if (effectiveBody is List<int>) {
      req.bodyBytes = effectiveBody;
    } else {
      final text = effectiveBody.toString();
      if (text.isEmpty) {
        // 显式清空 body(不设置 bodyBytes,避免发出 `Content-Length: 0` 的空壳)。
        // Explicitly clear the body (leave bodyBytes unset rather than sending
        // an empty `Content-Length: 0` shell).
      } else {
        req.body = text;
      }
    }
  }
  return req;
}