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 'image=@/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 'image=@/home/myuser/mypicture1.jpg' -F 'image=@/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 <.

Related :   Using cURL to do http GET and POST with JSON and XML data

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 password=@/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 “web=@index.html;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 “file=@localfile;filename=nameinpost” url.com If filename/path contains ‘,’ or ‘;’, it must be quoted by double-quotes like: curl -F “file=@\”localfile\”;filename=\”nameinpost\”” url.com or curl -F ‘file=@”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.


lynxbee_ezoic

Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


Leave a Comment

Android Android Applications Android Build system Android Commands Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded