API 总是错误 408 - 发送的数据类型无效

API always error 408 - Invalid Data Type Sent

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

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


这是我的结果:

来自邮差

来自我在 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,它会 return 一些错误(是的,认为它像任何其他主键 ID)。

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


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

Dart 的 package:http 在后台操纵 content-type header。如果它为您完成从字符串到字节的编码(即,如果 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 无效,但这很容易与您的工作邮递员进行比较。)