【问题标题】:API always error 408 - Invalid Data Type SentAPI 总是错误 408 - 发送的数据类型无效
【发布时间】:2020-04-26 02:16:48
【问题描述】:

我真的不知道这个错误会发生什么(我的意思是,这是否与 Flutter 或 Dart 或我使用的包或我使用的 API 有关)。简而言之,它总是给我返回错误代码 408 - 从我的颤振应用程序发送的数据类型无效,而我完全可以在 Postman 中完成它并使用 PHP 进行了尝试。

我只想向我的支付网关 API(我使用 Midtrans)调用一个正常的 POST 请求。


这是我的结果:

来自邮递员

来自我在 VS Code 中的 Flutter 应用


我做错了什么?我对此一无所知。 我将在下面为任何想要尝试简单调用此 API 的人提供详细信息,以便您可以重现此内容。

端点

https://api.sandbox.midtrans.com/v2/charge

身体

    "payment_type": "bank_transfer",
    "bank_transfer": {
        "bank": "permata",
        "va_number": "1234567890"
    },
    "transaction_details": {
        "order_id": "order-101b-{{2020-11-0222rrd324dzz}}",
        "gross_amount": 44000
    },
    "customer_details": {
        "email": "richie.permana@gmail.com",
        "first_name": "Richie",
        "last_name": "Permana",
        "phone": "+6281 1234 1234"
    },
    "item_details": [
        {
            "id": "item01",
            "price": 21000,
            "quantity": 1,
            "name": "Schema Programmer"
        },
        {
            "id": "item02",
            "price": 23000,
            "quantity": 1,
            "name": "Ayam Xoxoxo"
        }
    ]
}

这是标题

{
  'content-type': 'application/json',
  'accept': 'application/json',
  'authorization':
      'Basic U0ItTWlkLXNlcnZlci1kNnJNbGp5ZDBKSDgyOEhBT28tSWkxM0E=',
  'cache-control': 'no-cache'
}

我的 Dart 函数片段

Future _makePayment() async {
    String url = "https://api.sandbox.midtrans.com/v2/charge";

    final Map<String, String> header = {
      'content-type': 'application/json',
      'accept': 'application/json',
      'authorization':
          'Basic U0ItTWlkLXNlcnZlci1kNnJNbGp5ZDBKSDgyOEhBT28tSWkxM0E=',
      'cache-control': 'no-cache'
    };

    var json = jsonEncode({
      "payment_type": "bank_transfer",
      "bank_transfer": {"bank": "permata", "va_number": "1234567890"},
      "transaction_details": {
        "order_id": "order-101b-{{2020-11-0222rrddzz557}}",
        "gross_amount": 44000
      },
      "customer_details": {
        "email": "richie@example.com",
        "first_name": "Richie",
        "last_name": "Permana",
        "phone": "+6281 1234 1234"
      },
      "item_details": [
        {
          "id": "item01",
          "price": 21000,
          "quantity": 1,
          "name": "Schema Programmer"
        },
        {"id": "item02", "price": 23000, "quantity": 1, "name": "Ayam Xoxoxo"}
      ]
    });

    Response response = await post(
      url,
      headers: header,
      body: json,
    );


    var res = jsonDecode(response.body);
    print(
        "payloadddded =>> ${json.runtimeType}\n");
    if (res['status_code'] == 201) {
      print("ngeheeeee berhasil MIDTRAANSS =>> ${res['status_code']}");
    } else {
      print("res full =>> ${response.body}");
      print("ape kaden => ${res['status_code']} || terosz => $res");
    }
  }

注意:

对于正文,您可能需要更多地关注order_id,因为如果您不更改order_id,它将返回一些错误(是的,认为它就像任何其他主键ID)。

API 文档:https://api-docs.midtrans.com/


我非常感谢提供的任何帮助或解决方法。 非常感谢。

【问题讨论】:

    标签: http flutter dart payment-gateway


    【解决方案1】:

    Dart 的 package:http 在底层操纵 content-type 标头。如果它为您执行从字符串到字节的编码(即,如果 body 是一个字符串),那么它会在内容类型中添加一个 charset=xxx 后缀 - 例如,它变为 application/json; charset=utf-8

    显然您的服务器对此过敏。 (不应该,但无论如何......)。您可以使用dart:io http 客户端来证明这一点。

      var client = HttpClient();
      var request = await client.postUrl(Uri.parse(url));
      request.headers.set('content-type', 'application/json; charset=utf-8');
      request.headers.add(
        'authorization',
        'Basic U0ItTWlkLXNlcnZlci1kNnJNbGp5ZDBKSDgyOEhBT28tSWkxM0E=',
      );
      request.add(utf8.encode(json));
      var response2 = await request.close();
      var reply = await response2.transform(utf8.decoder).join();
      print(reply);
      client.close();
    

    这会产生相同的 408 错误,但如果删除字符集后缀则不会。

    有一个简单的解决方案。不要让http 为您进行编码 - 自己动手。

      var response = await http.post(
        url,
        headers: headers,
        body: utf8.encode(json),
      );
    

    (您的 json 显然需要工作,因为这现在抱怨 json 无效,但这很容易与您的工作邮递员进行比较。)

    【讨论】:

    • 我已经阅读了 VS Code 自动完成的迷你文档,它确实说将默认设置为 text/plain。之前,我一直想知道是不是我的 json 结构错误,但在我发现它之后,我也开始认为主要问题可能出现在标题部分。让我检查并尝试您的解决方案。非常感谢您的回复。
    • 你是对的。在我自己编码之后,它显然有效。太感谢了。由于这个问题,您刚刚为另一个 API 创建了一个 API,从而挽救了我的生命。也许这听起来很荒谬,但你能向我解释一下 API 如此严格的意义何在?他们甚至不接受任何其他标头(即:charset=xxx)而且我不知道我们应该在正文中将其编码为 utf8?
    • 表示 API 实现不佳。我在文档中看不到任何地方甚至说明要使用什么编码。想必我们对 utf8 的猜测是可以的。不允许后缀只是糟糕的设计。
    • 不错的答案@RichardHeap
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    相关资源
    最近更新 更多