How to Send POST Requests Using curl in Linux
If you work with APIs, servers, or web development in Linux, the curl command is one of the most useful tools you can learn. It allows you to send and receive data directly from the command line without using a browser. One of its most common and powerful uses is sending POST requests. In this guide, you will learn how to send POST requests using curl in Linux with easy examples and clear explanations.
1. What is curl in Linux
curl (Client URL) is a command-line utility that transfers data between a local machine and a remote server. It supports several protocols such as HTTP, HTTPS, FTP, and many others. Developers often use curl to test APIs, automate uploads, and debug network connections.
To check if curl is installed, use:
curl --versionIf it is not installed, you can install it with:
sudo apt install curl     # For Debian or Ubuntu
sudo yum install curl     # For CentOS or RHEL2. Basic Syntax for a POST Request
The general syntax for sending a POST request is:
curl -X POST [URL]This tells curl to send an HTTP POST request to the target URL. By itself, it sends an empty body, but you can attach data using the -d option.
3. Sending Form Data
When submitting data through a typical web form, you can simulate that process using curl:
curl -X POST -d "username=john&password=12345" https://example.com/loginHere, -d stands for data, and it sends form-encoded input by default. If needed, specify the content type explicitly:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "username=john&password=12345" https://example.com/loginThis is useful for testing web applications or APIs that accept form submissions.
4. Sending JSON Data
Most modern APIs use JSON data instead of traditional form data. To send JSON with curl, use the -H flag to define the header and -d to pass the payload:
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"Alice","email":"alice@example.com"}'Explanation:
-X POSTspecifies the HTTP method.-Hsets a custom header.-dsends data as a JSON string.
5. Adding Authentication and Custom Headers
Many APIs require authentication tokens or special headers. You can include them easily with multiple -H flags:
curl -X POST https://api.example.com/data \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_token_here" \
-d '{"key":"value"}'This allows you to simulate authorized requests, making curl an excellent tool for testing protected endpoints.
6. Sending Data from a File
If your JSON or form data is stored in a file, you can upload it using the @ symbol:
curl -X POST -H "Content-Type: application/json" -d @data.json https://api.example.com/uploadThe @data.json part tells curl to read the contents of data.json and send it as the request body.
7. Viewing and Debugging Responses
By default, curl prints the response body to the terminal. You can include response headers using:
curl -i -X POST ...To see detailed debugging information about the request and response process:
curl -v -X POST ...This is helpful for diagnosing connection issues or verifying that your data is being sent correctly.
Conclusion
Knowing how to send POST requests using curl in Linux gives you a powerful way to interact with web services, test APIs, and automate data transmission. You can easily send form data, JSON payloads, and authenticated requests all from your terminal. Once you master curl, you will find it an essential part of any Linux developer or administrator toolkit.
Refer curl documentation for more.
	







