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 "' 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'
To upload multiple files, run:
curl -F 'foo=@/path/to/img1.jpg' -F 'bar=@/path/to/img2.jpg'
In this example, I am uploading a file named ansible-setup.png to imgur:
curl -H "Referer:" -F "Filedata=@ansible-setup.png;type=image/png"
Sample outputs:


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

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....