Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a file to a python flask server using curl

Tags:

python

curl

flask

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.

like image 336
anonymous123 Avatar asked Jun 26 '13 19:06

anonymous123


People also ask

How do I upload files to a Flask server?

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 do I upload a file using curl?

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.

Can curl transfer files?

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.

How do I upload to FTP using curl?

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.


1 Answers

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.

like image 155
mata Avatar answered Sep 20 '22 11:09

mata