'How to send Binary encrypted data in flutter POST with request encoding :null ? ; which is working in node js properly

According to node.js Documentation encoding : null when binary data to be sent via Api,

https://www.npmjs.com/package/request in this link below mentioned explanation is found.

encoding - encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer. Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default).

Note: if you expect binary data, you should set encoding: null.

Now I have achieve the same thing in flutter/dart and this encoding parameter is not accepting null as here in node.js they have mentioned.

I want to know how to make this same Post request from Flutter/dart or at least android/java.

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);
                }
            });
        }
    });

};

Adding code the What i have tried in flutter

    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();

Even in post man this encoding variable is not present to set it.



Solution 1:[1]

My Final working code is

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}');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harish Kandekar