osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Transfer a file to httpserver via POST command from curl


I have written a python server app (actually copied from somewhere) using
Flask, that I want to act as a http server. I expect this server to recieve
a POST command (of a file) from CURL and save that file on the server. And
I should be able to download that file when required.

My python scripts are shown below.

app.py



from flask import Flask

UPLOAD_FOLDER = 'home/user/uploads'

app = Flask(__name__)
#app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024


main.py


import os
import urllib.request
from app import app
from flask import Flask, request, redirect, jsonify
from werkzeug.utils import secure_filename


ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])


def allowed_file(filename):
 return '.' in filename and filename.rsplit('.', 1)[1].lower() in
 ALLOWED_EXTENSIONS


@app.route('/file-upload', methods=['POST'])
def upload_file():
 # check if the post request has the file part
 if 'file' not in request.files:
 resp = jsonify({'message' : 'No file part in the request'})
 resp.status_code = 400
 return resp
 file = request.files['file']
 if file.filename == '':
 resp = jsonify({'message' : 'No file selected for uploading'})
 resp.status_code = 400
 return resp
 if file and allowed_file(file.filename):
 filename = secure_filename(file.filename)
 file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
 resp = jsonify({'message' : 'File successfully uploaded'})
 resp.status_code = 201
 return resp
 else:
 resp = jsonify({'message' : 'Allowed file types are txt, pdf, png, jpg,
jpeg, gif'})
 resp.status_code = 400
 return resp


if __name__ == "__main__":
    app.run()

I am doing a POST using curl as follows.

curl -X POST --data-binary @/home/user/testfile.txt http://
127.0.0.1:5000/file-upload

Is it really possible to transfer a large binary file from my machine to
the above httpserver via POST command and download it again? If yes, is the
above Flask app enough for that and what am I doing wrong?

Kindly Reply,
Regards,
Karthik.


I am getting 400 Bad Request error from the server. I am new to Flask.
What am I doing wrong. Is it possible to transfer a binary file
(large) from my computer to the above http server via POST command.?
For that purpose is my above code enought? What am I doing wrong?