How to use the cURL command to do a POST data to fields on Linux or Unix

How do I use the cURL command on Linux or Unix to post data i.e. upload something on Linux or Unix? How do I make a POST request with the cURL Linux command-line to upload file?

curl is a good tool to transfer data from or to a server especially making requests, testing requests and APIs from the command line. This tutorial shows how to upload files with curl command line on Linux or UNIX-like system.

Upload files with CURL

The syntax is:
curl -F "data=@file.name' https://url/name
To post data with fields:
curl --data "f=foo&d=bar" https://url/name
For example, if I want to upload a file at /home/petehouston/hello.txt to the server http://localhost/upload which processes file input with form parameter named img_avatar, I will make request like this,
curl -F 'img_name=@/home/nixcraft/welcome.jpg' https://server1.cyberciti.biz/cdnimages/
To upload multiple files, run:
curl -F 'foo=@/path/to/img1.jpg' -F 'bar=@/path/to/img2.jpg' https://server1.cyberciti.biz/form/
In this example, I am uploading a file named ansible-setup.png to imgur:
curl -H "Referer: https://imgur.com/upload" -F "Filedata=@ansible-setup.png;type=image/png" https://imgur.com/upload
Sample outputs:

{"data":{"hashes":["uZbpdKC"],"hash":"uZbpdKC","deletehash":"XXXXXXXX","album":false,"edit":false,"gallery":null,"animated":false,"height":112,"width":599,"ext":".png","msid":"8aaa97a2e5f6489cf6f65d086d47448b"},"success":true,"status":200

You can see uploaded url here. Pass the -v switch to see more info including progress:

Fig.01: curl command used to upload a file to imgur

See curl command man page for more information:
man curl
🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz