使用cURL将文件发布到Flask会返回400错误

时间:2016-10-12 18:19:59

标签: python curl flask

我想使用cURL将文件发布到Flask视图。但是,我得到400错误的响应。为什么下面的命令失败?

@app.route('/deploy/partial', methods=['POST'])
def postfile():
    try:
        file = request.files['file']
        filename = secure_filename(file.filename)
        file.save(os.path.join('/home/test/test.txt', filename))
        return 'OK', 200
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise
curl -X POST -H "Content-Type: multipart/form-data" -F "file=@FILEPATH" http://127.0.0.1:25252/deploy/partial

Unexpected error: <class 'werkzeug.exceptions.HTTPException.wrap.<locals>.newcls'>
127.0.0.1 - - [12/Oct/2016 22:57:43] "POST /deploy/partial HTTP/1.1" 400 

HTTP/1.1 100 Continue

HTTP/1.0 400 BAD REQUEST
Content-Type: text/html
Content-Length: 192
Server: Werkzeug/0.11.11 Python/3.4.3
Date: Wed, 12 Oct 2016 19:31:07 GMT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not    understand.</p>

1 个答案:

答案 0 :(得分:0)

我得到了

意外错误:.newcls&#39;&gt;

我意外地将request.form [&#39; name&#39;]重命名为request.form [&#39; ame&#39;] (注意错误的&#39; ame&#39;)而我的表格字段是 name = StringField(...)

我将request.form [&#39; ame&#39;]更改为request.form [&#39; name&#39;],错误消失,即烧瓶工作

不确定这是否也是你的情况。