Flutter Http 请求 Header Key 小写

问题描述 投票:0回答:2

在flutter中进行http post请求,header key变成全小写

user-agent: Dart/2.10 (dart:io)
content-type: application/json
accept-encoding: gzip
content-length: 46

有办法设置首字母大写吗?

User-Agent: Dart/2.10 (dart:io)
Content-Type: application/json
Accept-Encoding: gzip
Content-Length: 46

我发布请求的时候又把Header改了,服务端收到了,打印出来还是很小。

flutter dart post
2个回答
2
投票

我建议你使用Dio,在本例中,我使用Dio v.4.0。

  1. 在Dio包目录下找到io_adapter.dart文件,建议使用Intellij IDEA或Android Studio。

    io_adapter.dart file location

  2. 找到如下代码片段

    options.headers.forEach((k, v) => request.headers.set(k, v));
    
  3. 替换为此代码

    options.headers.forEach((k, v) {
     request.headers.set(k, v??"null",preserveHeaderCase: true);      });
    
  4. Flutter 清理并重建您的 Flutter 应用程序

  5. 完成

参考。 https://github.com/flutterchina/dio/issues/641


0
投票

在最新的 Dio 中,您可以通过选项轻松允许使用preserveHeaderCase 标志的区分大小写的标题键,例如:

Dio().options.preserveHeaderCase = true;
© www.soinside.com 2019 - 2024. All rights reserved.