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.

Share
Published by
0xSnow

Recent Posts

The Evolution of Cloud Technology: Where We Started and Where We’re Headed

Image credit:pexels.com If you think back to the early days of personal computing, you probably…

2 days ago

The Evolution of Online Finance Tools In a Tech-Driven World

In an era defined by technological innovation, the way people handle and understand money has…

2 days ago

A Complete Guide to Lenso.ai and Its Reverse Image Search Capabilities

The online world becomes more visually driven with every passing year. Images spread across websites,…

3 days ago

How Web Application Firewalls (WAFs) Work

General Working of a Web Application Firewall (WAF) A Web Application Firewall (WAF) acts as…

1 month ago

What Does chmod 777 Mean in Linux

If you are a Linux user, you have probably seen commands like chmod 777 while…

1 month ago

How to Undo and Redo in Vim or Vi

Vim and Vi are among the most powerful text editors in the Linux world. They…

1 month ago