如何从 Flutter 发送 unicode 到 Flask 服务器
How to send unicode to flask server from Flutter
我正在构建一个前端使用 Flutter,后端使用 python 的应用程序。
我要从flutter发送信息到python,比如:
{'english: 'Hello', 'hebrew':'שלום'}.
不幸的是,当我尝试这样做时,python returns io.UnsupportedOperation: not writable.
这是我发送信息的flutter代码:
() 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();
}
顺便说一句,我尝试了 utf8.encode 和不使用,都没有用。
这是接受方:
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
如何将 Unicode 发送到服务器端?
io.UnsupportedOperation: not writable
与unicode编码无关。您需要在 add
函数中打开一个可写文件。 open
默认为只读。我假设您还想附加到 data.csv
所以使用 a
标志。
def add(w1, w2):
with open('data.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow([w1, w2])
我正在构建一个前端使用 Flutter,后端使用 python 的应用程序。 我要从flutter发送信息到python,比如:
{'english: 'Hello', 'hebrew':'שלום'}.
不幸的是,当我尝试这样做时,python returns io.UnsupportedOperation: not writable.
这是我发送信息的flutter代码:
() 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();
}
顺便说一句,我尝试了 utf8.encode 和不使用,都没有用。
这是接受方:
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
如何将 Unicode 发送到服务器端?
io.UnsupportedOperation: not writable
与unicode编码无关。您需要在 add
函数中打开一个可写文件。 open
默认为只读。我假设您还想附加到 data.csv
所以使用 a
标志。
def add(w1, w2):
with open('data.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow([w1, w2])