I am trying to upload a file to a server using curl and python flask. Below I have the code of how I have implemented it. Any ideas on what I am doing wrong.
curl -i -X PUT -F name=Test -F [email protected] "http://localhost:5000/"
@app.route("/", methods=['POST','PUT'])
def hello():
file = request.files['Test']
if file and allowed_file(file.filename):
filename=secure_filename(file.filename)
print filename
return "Success"
The following is the error that the server sends back
<!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>
Thanks in advance.
Handling file upload in Flask is very easy. It needs an HTML form with its enctype attribute set to 'multipart/form-data', posting the file to a URL. The URL handler fetches file from request. files[] object and saves it to the desired location.
How to send a file using Curl? To upload a file, use the -d command-line option and begin data with the @ symbol. If you start the data with @, the rest should be the file's name from which Curl will read the data and send it to the server. Curl will use the file extension to send the correct MIME data type.
Uploading files using CURL is pretty straightforward once you've installed it. Several protocols allow CURL file upload including: FILE, FTP, FTPS, HTTP, HTTPS, IMAP, IMAPS, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, and TFTP. Each of these protocols works with CURL differently for uploading data.
To upload to an FTP server, you specify the entire target file path and name in the URL, and you specify the local file name to upload with -T, --upload-file . Optionally, you end the target URL with a slash and then the file component from the local path will be appended by curl and used as the remote file name.
Your curl command means you're transmitting two form contents, one file called filedata
, and one form field called name
. So you can do this:
file = request.files['filedata'] # gives you a FileStorage
test = request.form['name'] # gives you the string 'Test'
but request.files['Test']
doesn't exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With