通过curl命令或邮递员发送文件

时间:2017-03-28 19:27:59

标签: python curl flask

通过curl命令或邮递员发送文件

我正在尝试将图片上传到我的Flask应用程序,我能够通过网络浏览器上传按钮来做到这一点,但是当我向邮递员发出POST请求时,如果它给我一个错误303

127.0.0.1 - - [2017-03-28 20:16:05] "POST /api/user/uploadimg/ HTTP/1.1" 302 632 0.005147

这是我的Flask代码

@app.route('/api/user/uploadimg/', methods=['POST','GET'])
def upload_file():
    def allowed_file(filename):
        return '.' in filename and \
            filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
    # check if the post request has the file part

    if request.method == 'POST':
    # check if the post request has the file part
    if 'file' not in request.files:
        flash('No file part')
        return jsonify({"Nope":"No file entered"})
    file = request.files['file']
    # if user does not select file, browser also
    # submit a empty part without filename
    if file.filename == '':
        flash('No selected file')
        return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        user=User.query.get(7)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        user.image_filename=filename
        user.image_url=url_for(url_for('uploaded_file',filename=filename))
        db.session.commit()
        return jsonify({"Done":"All set!"})
    return '''
        <!doctype html>
        <title>Upload new File</title>
        <h1>Upload new File</h1>
        <form method=post enctype=multipart/form-data>
        <p><input type=file name=file>
        <input type=submit value=Upload>
        </form>'''

我很想知道如何卷曲以下网址并使用卷发或邮递员发送文件。  PS:我有邮递员以POST方式发送命令,内容类型是多部分数据

这是我用来发送数据的curl命令

curl -X POST -F "file=certificate.pdf" http://localhost:3500/api/user/uploadimg/ -H "Content-Type: multiform/form-data"

1 个答案:

答案 0 :(得分:2)

修正了它,在postman发送multiform数据时,不必指定Content类型的值。我不知道如何使用curl命令。