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

211

Solution 1

Use below flutter framework method

Future<Response> post(Uri url,
    {Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_withClient((client) =>
    client.post(url, headers: headers, body: body, encoding: encoding));

How to use

  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 

Solution 2

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}');
Share:
211
Harish Kandekar
Author by

Harish Kandekar

I am a Electrical and Electronics engineer by degree but interested in coding .. working as a senior Software engineer (Flutter | Android | iOS | Backend)

Updated on January 03, 2023

Comments

  • Harish Kandekar
    Harish Kandekar over 1 year

    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.

    • Benyamin
      Benyamin about 2 years
      try removing that line, just make it default.
    • Harish Kandekar
      Harish Kandekar about 2 years
      tried but API fails and returns 408 time out from flutter. how to send the binary data that is Uint8List to the POST api body, which is working with node.js code shown above with 200 ok response. but in flutter the same api returning 408