2017-03-28 49 views
-1

通過curl命令或郵遞員通過curl命令或郵遞員

我想上傳圖片到我的瓶的應用程序發送文件發送文件,我能夠sucesffuly做到這一點通過網絡瀏覽器上傳按鈕,然而,當我從我的郵遞員POST請求,如果它給我一個錯誤303

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

這裏是我的瓶代碼

@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>''' 

我很想知道我可以捲曲以下烏爾l並使用捲髮或郵遞員發送文件。 PS:我有郵遞員發送命令作爲POST方法,以及內容類型是多部分數據

這裏是捲曲命令我使用發送數據

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

你正在返回一個重定向,並且你得到一個重定向響應(302,而不是303)。你認爲什麼是錯的? – davidism

+0

我剛剛刪除了所有的重定向響應,並修復了郵件,顯然這個文件沒有上傳,但是在郵遞員中,我非常確定我已經選擇了正文部分,關鍵名稱是「文件」,並且文件已發送「 這就是我使用的curl命令。 curl -X POST -F」file = 2017-02-16 \ t2.51.40.jpg「http:// localhost:3500/api/user/uploadimg/-H「Content-Type:multiform/form-data」 – Abdul

+0

請在您的文章中加入[mcve] – davidism

回答

2

固定它,在郵遞員發送時多種形式的數據,不需要指定內容類型的值。我不知道如何用curl命令來做到這一點。