Linux

How to Send POST Requests Using curl in Linux

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 --version

If it is not installed, you can install it with:

sudo apt install curl     # For Debian or Ubuntu
sudo yum install curl     # For CentOS or RHEL

2. 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/login

Here, -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/login

This 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 POST specifies the HTTP method.
  • -H sets a custom header.
  • -d sends 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/upload

The @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.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

23 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

24 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

1 day ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

1 day ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

1 day ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

1 day ago