How to make a POST request using cURL on Ubuntu / Debian

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. The cURL command is a real Swiss army knife for network connections in the terminal. In this post, you will learn how to make a POST request with cURL. This way you will be able to quickly do some data sending tests.

Introduction—What is cURL?

According to the project website

cURL is command line tool and library for transferring data with URLs

So, it is a tool that does not have a graphical interface, but it is easy to use. In addition to this, it is open source, which allows auditing its source code.

cURL is available in almost any Linux distribution, and is even installed by default in some of them. So, it will be no problem to get it.

This tool is used in command lines or scripts to transfer data. Its application ranges from computers, of course, to cars, TVs and other devices that require it.

Let’s go.

Installing cURL on Ubuntu / Debian

As I mentioned earlier, cURL is available for almost any system, and the best thing is that from the official repositories. So to install it, open a terminal and run.

sudo apt update
sudo apt upgrade

It’s that quick and easy.

Now check the installed version

curl --version

Sample output

Making a POST request using cURL on Ubuntu / Debian
Making a POST request using cURL on Ubuntu / Debian

Now we can continue.

How to make a POST request using cURL on Ubuntu / Debian

The idea is to send data using HTTP POST. This is quite useful for scripts and configurations. In general, the syntax is as follows.

curl -X POST [options] [URL]

Not necessarily, but it is common that a POST request is made using HTML forms.

If you want to send some data like this, you can also use the -d option

curl -d "name=Angelo&website=unixcop" -X POST https://unixcop.com/

In this case, information is sent to a specific address. For this, the fields requested by the address are name and website. As you can see, then, some data is incorporated. For this, note that you need the exact address of the receiver of the data.

There is an -H option with which you can specify the format in which the data is to be sent. For example, if you want to send information to an HTML form with the format application/x-www-form-urlencoded then you have to specify it.

curl -X POST https://unixcop.com/form -H "Content-Type: application/x-www-form-urlencoded" -d "name=Angelo1&[email protected]" 

This way, cURL already knows in which format to deliver the data. Another example with this same parameter is in JSON format.

curl -X POST https://unixcop.com/json -H 'Content-Type: application/json' -d '{"name": "angelo", "password": "21125"}'.

I repeat, you have to know in which format the website will process the files before defining the curl command.

Another useful example is with XML file processing

curl -X POST https://reqbin.com/echo/post/xml -H "Content-Type: application/xml" -d "<Data><Id>1</Id><Username>unixcop</Username></Data>"

Or send a file. For this case, curl includes by default to send format depending on the file, but you have to specify the full path to the file. You can also specify a specific type with the -H option.

curl -d @[path] https://unixcop.com/server

As you can see, the process is basic.

Conclusion

In this post, you learned how to use cURL to send data using POST. Now I hope you liked it and you can apply it in your projects.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook