我真的不知道这个错误会发生什么(我的意思是,这与 Flutter 或 Dart 或我使用的包或我使用的 API 有关)。简而言之,它总是给我返回错误代码 408 - 从我的 flutter 应用程序发送的无效数据类型 ,而我完全可以在 Postman 中完成它并用 PHP 进行了尝试。
我只想向我的支付网关 API(我使用 Midtrans)调用一个正常的 POST 请求。
这是我的结果:
来自 postman
来自我在 VS Code 中 Flutter 中的应用程序
我究竟做错了什么?我对此一无所知。
我将在下面为任何想要尝试简单调用此 API 的人提供详细信息,以便您可以重现它。
端点 https://api.sandbox.midtrans.com/v2/charge
body
"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/
我非常感谢提供的任何帮助或解决方法。
非常感谢你。
最佳答案
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 无效,但这很容易与您的工作 postman 进行比较。)
关于http - API 总是错误 408 - 发送的数据类型无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59647647/