根據 node.js Documentation encoding : null 當二進制資料通過 Api 發送時,
https://www.npmjs.com/package/request在此鏈接中找到下面提到的解釋。
encoding - 用于回應資料的 setEncoding 的編碼。如果為 null,則正文作為緩沖區回傳。其他任何內容(包括 undefined 的默認值)都將作為編碼引數傳遞給 toString() (這意味著默認情況下這實際上是 utf8)。
注意:如果你期望二進制資料,你應該設定 encoding: null。
現在我在顫振/飛鏢中實作了同樣的事情,并且這個編碼引數不接受 null,就像他們在 node.js 中提到的那樣。
我想知道如何從 Flutter/dart 或至少 android/java 發出同樣的 Post 請求。
var enc = AESCrypt.encrypt(key, iv, JSON.stringify(obj_j));
var output = new Buffer.from(enc, 'hex'); // Buffer
function test() {
console.time("XXX");
request.post({
headers: {
'content-type': 'application/json'
}, //required, or webserver will ignore it application/json multipart/form-data
url: 'http://192.168.29.210/deviceid/read', // webserver url
encoding:null,
body: output
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.timeEnd("XXX");
body = AESCrypt.decrypt(key, iv, body);
//body is decrypted http response, can be parsed with json method
fs.writeFile('input.json', body, function (err) {
if (err) {
return console.error(err);
}
});
}
});
};
添加代碼我在顫振中嘗試過的內容
var headers = {'Content-Type': 'application/json'};
var request =
http.Request('POST', Uri.parse('http://192.168.29.210/deviceid/read'));
request.body = encryptedText;
request.encoding = null ; // here this null parameter is not acceptable
request.encoding = Encoding.getByName("utf-8")); // only this option is available to add in flutter
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
即使在郵遞員中,也不存在此編碼變數來設定它。
uj5u.com熱心網友回復:
使用下面的顫振框架方法
Future<Response> post(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_withClient((client) =>
client.post(url, headers: headers, body: body, encoding: encoding));
如何使用
final url = Uri.parse('$urlPrefix/posts');
final headers = {"Content-type": "application/json"};
final json = '{"title": "Hello", "body": "body text", "userId": 1}';
final response = await post(url, headers: headers, body: json,encoding:null); //here this null parameter is not acceptable
uj5u.com熱心網友回復:
我的最終作業代碼是
var headers = {'Content-Type': 'application/json'};
final response = await http.post(
Uri.parse('http://192.168.29.210/deviceid/read'),
headers: headers,
body: encryptedText,
encoding: null);
if (response.statusCode == 200) {
String res = response.body.toString();
//String data = AesEncryption().decryption(res);
print('Body: ${response.body.toString()}');
} else {
print(response.reasonPhrase);
}
print('Status code: ${response.statusCode}');
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/423056.html
標籤:
下一篇:如何同時從串列中迭代和洗掉元素?
