【问题标题】:Flask is not encoding form correctly from culFlask 没有从 cul 正确编码形式
【发布时间】:2018-11-28 16:11:06
【问题描述】:

我有一个带有一个端点的简单烧瓶应用程序

@app.route('/', methods=['GET', 'POST'])
def index():
  if request.method == 'GET':
    return render_template('form.html')
  return request.form['a']

form.html 看起来像这样:

<!DOCTYPE html>
<form method="POST">
  <input type="text" name="a" value="öäü"><br>
  <input type="submit" value="submit">
</form>

当我在 Firefox 中打开表单时,我可以输入 äöü 之类的值,发送 POST 请求结束接收正确的响应 äöü

但是,当我尝试像这样使用 curl 发送 POST 请求时:

curl http://localhost -F "a=öäü"

我得到了神秘的回应:

´┐¢´┐¢´┐¢

我也试过How do I POST form data with UTF-8 encoding by using curl?的解决方案

curl -v -X POST -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" --data-ascii "a=äöü" http://localhost

但我仍然得到相同的结果。我在 Windows 7 上使用 curl,代码页为 850

对于如何调试此行为的任何帮助,我们深表感谢

【问题讨论】:

    标签: python python-3.x curl flask character-encoding


    【解决方案1】:

    您可以对数据进行urlencode:

    curl http://localhost -F "a%3D%C3%B6%C3%A4%C3%BC"
    

    要自动执行此操作,curl 具有 --data-urlencode 标志。

    【讨论】:

    • --data-urlencode 提供与-F 相同的输出。 curl http://localhost -F "a%3D%C3%B6%C3%A4%C3%BC" 给出输出 Warning: Illegally formatted input field! curl: option -F: is badly used here
    • 根据您的解决方案,我尝试了这种方式:curl -v -X POST -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" --data-raw "a=%C3%B6%C3%A4%C3%BC" http://localhost 效果很好。但是我仍然在纠结如何从äöü 生成字符串%C3%B6%C3%A4%C3%BC
    【解决方案2】:

    我不是windows pc,无法确认,你可以试试curl -X POST -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" --data-urlencode "a=öäü" http://localhost

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-24
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 2016-04-08
      • 2015-09-19
      • 1970-01-01
      相关资源
      最近更新 更多