我得到了一個非常令人沮喪的經歷。我正在向 api 發出發布請求,在郵遞員上效果很好,但拒絕在顫振上作業。

這是錯誤回應,它告訴我這是標題的問題。
{"message":"Missing/Incorrect Security Key Header","apiVersion":"v1","transactionRef":"N202204281825312267","data":null,"error":{"code":401,"message":"Authorization failed for this request!"}}
我的請求
static Future<dynamic> postRequest(String url) async {
http.Response response = await http.post(Uri.parse(url),
headers: <String, String>{
"Access-Key" : "6a637874-0394-4d9d-a803-86323c1ddc4d",
"Accept": "application/json",
"Content-Type" : 'application/json',
},
);
print(response.body);
print(url);
try {
if (response.statusCode == 200) {
String data = response.body;
var decodedData = jsonDecode(data);
return decodedData;
} else {
return response.body;
}
} catch (e) {
return response.body;
}
}
uj5u.com熱心網友回復:
我想向您介紹 Flutter 中的另一個包,它可以更輕松地處理 http 請求,它稱為 Dio。一切都一樣,它還默認洗掉了一些樣板,你不必決議 url,你可以直接傳遞 url 字串。您不必編碼/解碼 json。dio 不是在 http 中傳遞 headers 屬性,而是按以下方式作業,
將依賴項添加到 pubspec.yaml 檔案并匯入 dio.dart 檔案。
說明可以在這里找到https://pub.dev/packages/dio
這是一個例子:
Future fetchData () async {
Dio dio = new Dio();
String url = 'example.com';
final response = await dio.post(
url,
options: Options(
headers: {'Access-Key': 'key'},
);
print(response.data);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467994.html
