【问题标题】:API works fine in Postman but throws 401 UnauthorizedException in Flutter appAPI 在 Postman 中运行良好,但在 Flutter 应用程序中抛出 401 UnauthorizedException
【发布时间】:2022-08-19 19:11:28
【问题描述】:

我有一个基于令牌的 API,它在 Postman(或 soapUI)中运行良好,但在通过 Flutter 应用程序点击时抛出 UnauthorizedException。这是发布请求:

import \'package:http/http.dart\' as http;

final response = await http.post(
        Uri.parse(url),
        headers: {
      \'Content-Type\': \'application/json\',
      \'Accept\': \'application/json\',
      \'Authorization\': \'Bearer $token\',
    };,
        body: jsonEncode(body),
      );

经过大量挖掘,dart 的http 客户端似乎正在将标头标签转换为小写,如日志所示:

<tran:user-header   name=\"authorization\"   value=\"Bearer eyJhbGciOiJSU...\" />
<http:User-Agent>Dart/2.17 (dart:io)</http:User-Agent>

而使用okhttp 客户端(java)发送的相同发布请求记录:

<tran:user-header   name=\"Authorization\"   value=\"Bearer eyJhbGciOiJSU...\" />
<http:User-Agent>okhttp 4.9.0</http:User-Agent>

我还尝试了 dart 的 dio 包,但问题仍然存在。真的是这样还​​是我只是在假设一些事情?

    标签: flutter api http okhttp


    【解决方案1】:

    标题名字不区分大小写。所以这不是(不应该是)你面临的问题。

    标头值(当然)可以区分大小写,但不会转换为小写。

    【讨论】:

      猜你喜欢
      • 2020-03-05
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2021-01-18
      • 1970-01-01
      • 2017-11-16
      • 2012-11-05
      相关资源
      最近更新 更多