How to upload file / image using CURL command line

If you want to upload some file or image from ubuntu curl command line utility, its very easy !

CURL provides a simplest form of syntax for uploading files, “-F” option available with curl emulates a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data.

Uploading single file / image

Lets say, we want to upload an picture available at “/home/myuser/mypicture.jpg” to server at “https://www.YOUR_SERVER_URL/upload” which uses file input form with parameter name as “image”, then we will have to use below command,

 $ curl -k -X POST -F '[email protected]/home/myuser/mypicture.jpg' -v  https://www.YOUR_SERVER_URL/upload/ 

here, above -v option is just for debugging purpose to understand how it goes with more verbose messages, you can avoid using it in production.

Uploading multiple files

For uploading multiple files, just add another “-F” option along with the same command as above, like

 $ curl -k -X POST -F '[email protected]/home/myuser/mypicture1.jpg' -F '[email protected]/home/myuser/mypicture2.jpg' -v  https://www.YOUR_SERVER_URL/upload/ 
 
"-F" option with curl command enables  uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload, while the < makes a text field and just get the  contents for that text field from a file.

Example, to send your password file to the server, where 'password' is the name of the form-field to which /etc/passwd will be the input:
curl -F [email protected]/etc/passwd www.mypasswords.com

To read content from stdin instead of a file, use - as the filename. This goes for both @ and < constructs. Unfortunately it does not support reading the file from a named pipe or similar, as it needs the full size before the transfer starts.

You can also tell curl what Content-Type to use by using 'type=', in a manner similar to:
curl -F "[email protected];type=text/html" url.com
or
curl -F "name=daniel;type=text/foo" url.com

You can also explicitly change the name field of a file upload part by setting filename=, like this:
curl -F "[email protected];filename=nameinpost" url.com

If filename/path contains ',' or ';', it must be quoted by double-quotes like:
curl -F "[email protected]\"localfile\";filename=\"nameinpost\"" url.com
              or
curl -F '[email protected]"localfile";filename="nameinpost"' url.com

Note that if a filename/path is quoted by double-quotes, any double-quote or backslash within the filename must be escaped by backslash.
Follow Lynxbee
Tags:

Leave a Reply / Ask Question