Home > Back-end >  How to send unicode to flask server from Flutter
How to send unicode to flask server from Flutter

Time:12-03

I'm building an app with Flutter on front, and python on back. I have to send information from flutter to python, such as {'english': 'Hello', 'hebrew':'שלום'}. Unforttunately, when I try to do so, python returns io.UnsupportedOperation: not writable.

This is my flutter code to send information:

() async{
      var encoded = utf8.encode(hebrewController.text);
      Map<String, String> body = {'english': englishController.text, 'hebrew': encoded.toString()};
      String jsonString = json.encode(body);

      var response = await http.post(
        Uri.parse("http://192.168.80.46:8080/add"),
        body: jsonString,
        encoding: Encoding.getByName('UTF-8')
      );
      if(response.statusCode == 200){
        showSnackBar("Succesfully added new pair of words");
      }else{
        showSnackBar("Something went wrong...");
      }
      Navigator.of(context).pop();
    }

BTW, I tried both with utf8.encode and without, nothing works.

And this is the accepting side:

def add():
   english_word = request.args.get("english")
   hebrew_word = request.args.get("hebrew")
   scripts.add(english_word, hebrew_word)

scripts.add:

def add(w1, w2):
with open('data.csv') as f:
    writer = csv.writer(f)
    writer.writerow([w1, w2])
    f.close

How can I send a unicode to server side?

CodePudding user response:

io.UnsupportedOperation: not writable is unrelated to unicode encoding. You need to open a writable file in your add function. open defaults to read-only. I assume you also want to append to data.csv so use the a flag.

def add(w1, w2):
    with open('data.csv', 'a') as f:
        writer = csv.writer(f)
        writer.writerow([w1, w2])
  • Related