python - Flask request.get_data() 不处理 UTF-8 字符
问题描述
我已经用 Python 编写了一个简单的 Flask Web 服务,它需要带有字符串作为数据的 HTTP POST 请求。
@app.route('/hello', methods=['POST'])
def hello():
json_response = {}
data = request.get_data(as_text=True)
json_response['data'] = data
return json_response
当我尝试使用 cURL 使用 ASCII 字符串调用它时,它工作正常:
curl 127.0.0.1:5000/hello -X POST -H "Content-Type: text/plain" -d "hello world"
{
"data": "hello world"
}
但是,当我尝试使用包含 UTF-8 字符的字符串调用它时,无法正确读取 UTF-8 字符,而是替换为替换字符(就像decode('utf-8', 'replace')
会一样):
curl 127.0.0.1:5000/hello -X POST -H "Content-Type: text/plain" -d "hello €"
{
"data": "hello \ufffd"
}
我认为重点get_data(as_text=True)
是将数据作为 Unicode 字符串读取?我在这里错过了什么吗?
解决方案
推荐阅读
- c# - 使用 EF Core 2.1 继承进行过滤
- python - Ctypes DLL 使用 c_char_Array 调用 ArgumentError
- android - 当我的移动应用程序未连接到同一网络时,无法访问我的网络服务?
- apache-kafka - org.apache.kafka.connect.errors.DataException:数组默认值的 JSON 无效:“null”
- java - 如何在java swing中将文本打印到热敏打印机的参数中设置发票
- php - 未找到 laravel 模型中的搜索问题
- node.js - 无法从 http.js 文件配置 bodyParser 限制,因此得到 413 http 错误
- java - Java XML DOM:为什么 Java XML Attr.isId() 在应该返回 true 时返回 false
- gem5 - 如何在 gem 5 中运行我自己的程序?假设我想对 1 千个数字进行排序,因为我想制作程序然后想运行?
- javascript - JavaScript/jQuery 2 下拉菜单在页面加载时具有相同的选择选项