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

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"
curl -F "name=daniel;type=text/foo"

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

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

Note that if a filename/path is quoted by double-quotes, any double-quote or backslash within the filename must be escaped by backslash.
Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question